-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-text-editor.cpp
74 lines (62 loc) · 1.32 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* @file simple-text-editor.cpp
* @author Alexander Khvolis(ExpliuM)
* @link https://www.hackerrank.com/challenges/simple-text-editor/problem?isFullScreen=true
* @version 0.1
* @date 2023-02-19
*
* @copyright Copyright (c) 2023
*
*/
#include <iostream>
#include <stack>
using namespace std;
enum ACTION
{
APPEND = 1,
DELETE = 2,
PRINT = 3,
UNDO = 4
};
int main()
{
string s;
string stringToAppend;
stack<string> history;
int length;
int numberOfQueries;
cin >> numberOfQueries;
for (; numberOfQueries > 0; --numberOfQueries)
{
int action;
cin >> action;
switch (action)
{
case APPEND:
history.push(s);
cin >> stringToAppend;
s.append(stringToAppend);
break;
case DELETE:
history.push(s);
cin >> length;
s.resize(s.size() - length);
break;
case PRINT:
cin >> length;
cout << s.c_str()[length - 1] << endl;
break;
case UNDO:
if (!history.empty())
{
s = history.top();
history.pop();
}
break;
default:
cout << "error" << endl;
return -1;
}
}
return 0;
}