-
Notifications
You must be signed in to change notification settings - Fork 111
/
solution.cpp
72 lines (72 loc) · 2.4 KB
/
solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<bits/stdc++.h>
using namespace std;
int main(){
int q;
cin>>q;
vector<string>undo_erase;
//This holds the strings that we will be need to append if the erase function needs to be undone
vector<int>undo_append;
//This holds the size of the strings that we will need to erase if the append function needs to be undone
string curr;
//This holds the current string
vector<int>state;
//This holds the state of the last operation
//1 : append
//2 : erase
for(int i=0;i<q;i++){
int ch;
cin>>ch;
//This variable holds the type of the operation
if(ch==1){
string s;
cin>>s;
curr.append(s);
//Appending the input string to the current string
undo_append.push_back(s.size());
//Storing the size of the appended string
state.push_back(1);
//Storing the state of the operation
}
else if(ch==2){
int k;
cin>>k;
string temp;
while(k--){
temp.push_back(curr.back());
curr.pop_back();
}
//Erasing k elements of the current string
reverse(temp.begin(), temp.end());
undo_erase.push_back(temp);
//Storing the erased elements in a vector of strings
state.push_back(2);
//Storing the state of the operation
}
else if(ch==3){
int k;
cin>>k;
cout<<curr[k-1]<<endl;
//Printing the kth element of the current string
}
else{
int k=state.back();
state.pop_back();
//Retrieving the state of the last operation and erasing it
if(k==1){
//If the last operation was an append operation
int x=undo_append.back();
undo_append.pop_back();
while(x--){
curr.pop_back();
}
//Deleting x(number of elements appended in the last operation) elements from the current string
}
else{
//If the last operation was an erase operation
curr.append(undo_erase.back());
undo_erase.pop_back();
//Appending the string (erased in the last operation) to the current string
}
}
}
}