Skip to content

Commit 7b020c4

Browse files
authored
[20260106] BOJ / G2 / 시간 여행 / 한종욱
1 parent 28c57bf commit 7b020c4

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
```
2+
import java.io.*;
3+
import java.util.Objects;
4+
import java.util.StringTokenizer;
5+
6+
public class Main {
7+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
9+
private static Node[] history;
10+
private static int N;
11+
12+
public static void main(String[] args) throws IOException {
13+
init();
14+
15+
for (int i = 1; i <= N; i++) {
16+
StringTokenizer st = new StringTokenizer(br.readLine());
17+
char q = st.nextToken().charAt(0);
18+
19+
if (q == 'a') {
20+
int val = Integer.parseInt(st.nextToken());
21+
history[i] = new Node(val, history[i-1]);
22+
} else if (q == 's') {
23+
history[i] = history[i-1].prev;
24+
} else {
25+
int time = Integer.parseInt(st.nextToken());
26+
history[i] = history[time-1];
27+
}
28+
29+
if (Objects.isNull(history[i])) bw.write("-1" + "\n");
30+
else bw.write(history[i].val + "\n");
31+
}
32+
bw.flush();
33+
bw.close();
34+
br.close();
35+
}
36+
37+
private static void init() throws IOException {
38+
N = Integer.parseInt(br.readLine());
39+
history = new Node[N + 1];
40+
41+
history[0] = null;
42+
}
43+
44+
static class Node {
45+
int val;
46+
Node prev;
47+
48+
public Node(int val, Node prev) {
49+
this.val = val;
50+
this.prev = prev;
51+
}
52+
}
53+
}
54+
```

0 commit comments

Comments
 (0)