-
Notifications
You must be signed in to change notification settings - Fork 2
/
최하라.java
74 lines (66 loc) · 2.42 KB
/
최하라.java
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
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
int T = Integer.parseInt(br.readLine());
for (int t = 0; t < T; t++) {
String instruction = br.readLine();
// 배열 추려내기
int N = Integer.parseInt(br.readLine());
String strArr = br.readLine();
String[] toArr = strArr.substring(1, strArr.length() - 1).split(",");
// 배열 수들을 디큐에 저장
Deque<String> arrDequeue = new ArrayDeque<>();
for (int i = 0; i < N; i++)
arrDequeue.add(toArr[i]);
// 수행함수
boolean right = false;
boolean possible = true;
for (int i = 0; i < instruction.length(); i++) {
if (!possible)
break;
switch (instruction.charAt(i)) {
case 'R':
right = !right;
break;
case 'D':
if (arrDequeue.isEmpty()) { // 디큐가 비었을 때
possible = false;
break;
}
// 숫자 존재
if (right)
arrDequeue.pollLast();
else
arrDequeue.pollFirst();
}
}
// 결과 출력
if (!possible)
sb.append("error\n");
else {
sb.append("[");
if (arrDequeue.isEmpty()) {
sb.append("]\n");
continue;
}
while (!arrDequeue.isEmpty()) {
if (right)
sb.append(arrDequeue.pollLast()); // flip
else
sb.append(arrDequeue.pollFirst());
sb.append(",");
}
sb.replace(sb.length() - 1, sb.length(), "]");
sb.append("\n");
}
}
bw.write(sb.toString());
bw.flush();
bw.close();
br.close();
}
}