-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_text_editor.java
More file actions
158 lines (118 loc) · 4.2 KB
/
Copy pathsimple_text_editor.java
File metadata and controls
158 lines (118 loc) · 4.2 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
In this challenge, you must implement a simple text editor. Initially, your editor contains an empty string, . You must 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.
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
8
1 abc
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.
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static boolean prev_del = false;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num_operations = scan.nextInt();
Stack<String> stack = new Stack<String>();
for(int i = 0; i < num_operations; i++) {
int op_type = scan.nextInt();
switch(op_type) {
//append
case 1:
append(scan, stack);
break;
//delete
case 2:
delete(scan, stack);
break;
//print
case 3:
print(scan, stack);
break;
//undo
case 4:
undo(stack);
break;
}
}
}
public static void append(Scanner scan, Stack<String> stack) {
String str = scan.next();
String input;
if(!stack.isEmpty()) {
input = stack.peek() + str;
} else {
input = str;
}
stack.push(input);
return;
}
public static void delete(Scanner scan, Stack<String> stack) {
int num_del = scan.nextInt();
String old_str, new_str;
if(!stack.isEmpty()) {
old_str = stack.peek();
} else {
old_str = "";
}
new_str = old_str.substring(0, old_str.length() - num_del);
stack.push(new_str);
return;
}
public static void print(Scanner scan, Stack<String> stack) {
int pos = scan.nextInt();
if(!stack.isEmpty()) {
String topInput = stack.peek();
System.out.println(topInput.charAt(pos - 1));
}
return;
}
public static void undo(Stack<String> stack) {
String s = stack.pop();
return;
}
public static void print(Stack<String> stack) {
Iterator iter = stack.iterator();
System.out.print("STACK: ");
while(iter.hasNext()) {
System.out.print("[" + iter.next() + "] ");
}
System.out.println();
}
}