Simple Text Editor

The editor contains an empty string, S. You must perform  operations of the following  types:

  1. append - Append the string W to the end of S.
  2. delete - Delete the last k characters of S.
  3. print - Print the kth character of S.
  4. undo - Undo the last (not previously undone) operation of type 1 or 2, reverting S to the state it was in prior to that operation.

Input:

8
1 abc
3 3
2 3
1 xy
3 2
4
4
3 1

Output:

c
y
a

Approach:

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int q;
    cin >> q;
    stack<stringst;
    string s = "";
    st.push(s);
    int p = 0;
    while (q--)
    {
        int x;
        cin >> x;
        if (x == 1)
        {
            string str;
            cin >> str;
            s.append(str);
            st.push(s);
        }
        else if (x == 2)
        {
            int k;
            cin >> k;
            s.erase(s.size() - kk);

            st.push(s);
        }
        else if (x == 3)
        {
            int k;
            cin >> k;
            cout << s[k - 1] << "\n";
        }
        else if (x == 4)
        {
            st.pop();
            s = st.top();
        }
    }
    return 0;
}


No comments:

Post a Comment