728x90
틀렸습니다 나왔음 반례
1
RR
0
[]
출력 : ]
if(sBuilder.charAt(sBuilder.length()-1)==',') {
sBuilder.setLength(sBuilder.length()-1);
}
로 고쳐줌.. 마지막 숫자 쉼표 떼주기였는데 element가 없을 때 대괄호 []에서 앞에 대괄호를 떼버리는듯
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
|
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Deque;
import java.util.LinkedList;
import java.util.StringTokenizer;
public class Main {
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException{
int T = Integer.parseInt(br.readLine());
StringBuilder sBuilder = new StringBuilder();
Loop1:
for(int i=0; i<T; i++) {
String functions = br.readLine();
int n = Integer.parseInt(br.readLine());
String numArr = br.readLine().replaceAll("[ \\[|\\] ]","");
StringTokenizer stk = new StringTokenizer(numArr,",");
Deque<Integer> deque = new LinkedList<Integer>();
boolean reverse = false;
while(stk.hasMoreTokens()) {
deque.add(Integer.parseInt(stk.nextToken()));
}
for(int j=0; j<functions.length(); j++) {
if(functions.charAt(j)=='R') reverse = !reverse;
else if(functions.charAt(j)=='D') {
if(deque.size()<=0) {
sBuilder.append("error\n");
continue Loop1;
}
else if(reverse) deque.pollLast();
else deque.pollFirst();
}
}
sBuilder.append("[");
while(deque.size()>0) {
if(reverse) sBuilder.append(deque.pollLast()+",");
else sBuilder.append(deque.pollFirst()+",");
}
if(sBuilder.charAt(sBuilder.length()-1)==',') {
sBuilder.setLength(sBuilder.length()-1);
}
sBuilder.append("]\n");
}
bw.write(sBuilder.toString());
bw.flush();
bw.close();
}
}
|
cs |
'알고리즘 풀이 > 백준' 카테고리의 다른 글
★ [백준][Java] 2504번 괄호의 값(Stack) #2 (0) | 2021.09.11 |
---|---|
[백준][Java] 1966번 프린터 큐 (Queue, LinkedList) (0) | 2021.09.11 |
[백준][Java] 1032번 명령 프롬프트 (String) (0) | 2021.09.09 |
[백준][Java] 4949번 균형잡힌 세상 (String, stack) (0) | 2021.09.08 |
[백준][Java] 11404번 플로이드 (플로이드 와샬, DP) (0) | 2021.09.08 |