Cerinta completa
Implement a simple text editor. The editor initially contains an empty string, . Perform operations of the following types:
- append – Append string to the end of .
- delete – Delete the last characters of .
- print – Print the character of .
- undo – Undo the last (not previously undone) operation of type or , reverting to the state it was in prior to that operation.
Example
operation index S ops[index] explanation ----- ------ ---------- ----------- 0 abcde 1 fg append fg 1 abcdefg 3 6 print the 6th letter - f 2 abcdefg 2 5 delete the last 5 letters 3 ab 4 undo the last operation, index 2 4 abcdefg 3 7 print the 7th characgter - g 5 abcdefg 4 undo the last operation, index 0 6 abcde 3 4 print the 4th character - d
The results should be printed as:
f
g
d
Input Format
The first line contains an integer, , denoting the number of operations.
Each line of the subsequent lines (where ) defines an operation to be performed. Each operation starts with a single integer, (where ), denoting a type of operation as defined in the Problem Statement above. If the operation requires an argument, is followed by its space-separated argument. For example, if and , line will be 1 abcd.
Constraints
- The sum of the lengths of all in the input .
- The sum of over all delete operations .
- All input characters are lowercase English letters.
- It is guaranteed that the sequence of operations given as input is possible to perform.
Output Format
Each operation of type must print the character on a new line.
Sample Input
STDIN Function ----- -------- 8 Q = 8 1 abc ops[0] = '1 abc' 3 3 ops[1] = '3 3' 2 3 ... 1 xy 3 2 4 4 3 1
Sample Output
c
y
a
Explanation
Initially, is empty. The following sequence of operations are described below:
- . We append to , so .
- Print the character on a new line. Currently, the character is
c. - Delete the last characters in (), so .
- Append to , so .
- Print the character on a new line. Currently, the character is
y. - Undo the last update to , making empty again (i.e., ).
- Undo the next to last update to (the deletion of the last characters), making .
- Print the character on a new line. Currently, the character is
a.
Limbajul de programare folosit: cpp14
Cod:
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <limits>
#include <tuple>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
using namespace std;
typedef struct query
{
int type, remove;
string add;
}query;
char stak[1000003];
int topp = 0;
void push(string s)
{
for(int i = 0 ; i < s.length(); i++)
{
stak[++topp] = s[i];
}
assert(topp <= 1000000);
}
string pop(int remove)
{
assert(remove >= 0 && remove <= topp);
string popped = "";
int del = remove;
for (int i = topp; del > 0; i--, del--) {
popped = stak[i] + popped;
}
topp -= remove;
return popped;
}
int main()
{
int t, type, remove, k;
stack<query> q_stack;
cin>>t;
while(t--)
{
cin>>type;
if(type == 1)
{
string add;
cin>>add;
push(add);
query last;
last.type = type;
last.add = add;
q_stack.push(last);
}
else if(type == 2)
{
cin>>remove;
string popped = pop(remove);
query last;
last.type = type;
last.remove = remove;
last.add = popped;
q_stack.push(last);
}
else if(type == 3)
{
cin>>k;
assert(k >= 1 && k <= topp);
cout<<stak[k]<<endl;
}
else
{
query last = q_stack.top();
q_stack.pop();
if(last.type == 1)
{
int remove = (last.add).length();
string popped = pop(remove);
}
else
{
push(last.add);
assert(topp >= 1 && topp <= 1000000);
}
}
}
return 0;
}
Scor obtinut: 1.0
Submission ID: 464653243
Link challenge: https://www.hackerrank.com/challenges/simple-text-editor/problem
