forked from recongamer/Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simple Text Editor.cpp
59 lines (54 loc) · 1.29 KB
/
Simple Text Editor.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
// Implement a simple text editor. The editor initially contains an empty string 's'. Perform 'q' operations of the following 4 types:
// 1.append(w) - Append string 'w' to the end of 's'.
// 2.delete(k) - Delete the last 'k' characters of 's'.
// 3.print(k) - Print the kth character of 's'.
// 4.undo() - Undo the last (not previously undone) operation of type or , reverting 's' to the state it was in prior to that operation.
#include <bits/stdc++.h>
using namespace std;
int main() {
int q;
cin>>q;
string s;string r,e;
stack<int> st;
while(q--)
{
int t;
cin>>t;
st.push(t);
if(t==1)
{
cin>>r;
s=s+r;
}
else if (t==2) {
int k;
cin>>k;
for(int i=s.length()-1;i>=s.length()-k;i--)
{e=s[i]+e;
s[i]='\0';
}
}
else if(t==3)
{
int l;
cin>>l;
cout<<s[l-1]<<endl;
}
else {
if(st.top()==1)
{
for(int i=s.length()-1;i>=s.length()-r.length();i--)
{
s[i]='\0';
}
st.pop();
}
else if(st.top()==2)
{
s=s+e;
st.pop();
}
}
}
return 0;
}