Skip to content

Commit 8a69754

Browse files
committed
BOJ #5430: AC
1 parent 8935797 commit 8a69754

File tree

1 file changed

+68
-61
lines changed

1 file changed

+68
-61
lines changed

BOJ/5430/Main.java

Lines changed: 68 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,69 @@
1-
/*
2-
* Author: Kim Min-Ho (ISKU)
3-
* Date: 2017.03.05
4-
* Email: minho1a@hanmail.net
5-
*
6-
* https://github.com/ISKU/Algorithm
7-
* https://www.acmicpc.net/problem/5430
8-
*/
9-
10-
import java.util.Scanner;
11-
import java.util.ArrayList;
12-
13-
public class Main {
14-
public static void main(String... args) {
15-
Scanner input = new Scanner(System.in);
16-
int testCase = input.nextInt();
17-
18-
while (testCase-- > 0) {
19-
char[] command = input.next().toCharArray();
20-
int size = input.nextInt();
21-
ArrayList<Integer> array = new ArrayList<Integer>();
22-
String[] value = input.next().replaceAll("\\[|\\]", "").split(",");
23-
24-
for (int i = 0; i < size; i++)
25-
array.add(Integer.parseInt(value[i]));
26-
27-
boolean reverse = true;
28-
boolean check = false;
29-
for (int i = 0; i < command.length; i++) {
30-
if (command[i] == 'R')
31-
reverse = !reverse;
32-
33-
if (command[i] == 'D') {
34-
if (array.size() != 0) {
35-
if (reverse)
36-
array.remove(0);
37-
else
38-
array.remove(array.size() - 1);
39-
} else {
40-
check = true;
41-
break;
42-
}
43-
}
44-
}
45-
46-
if (check)
47-
System.out.println("error");
48-
else {
49-
if (reverse) {
50-
System.out.print("[" + (array.size() != 0 ? array.get(0) : ""));
51-
for (int i = 1; i < array.size(); i++)
52-
System.out.print("," + array.get(i));
53-
} else {
54-
System.out.print("[" + (array.size() != 0 ? array.get(array.size() - 1) : ""));
55-
for (int i = array.size() - 2; i >= 0; i--)
56-
System.out.print("," + array.get(i));
57-
}
58-
System.out.println("]");
59-
}
60-
}
61-
}
1+
/*
2+
* Author: Minho Kim (ISKU)
3+
* Date: February 10, 2020
4+
* E-mail: minho.kim093@gmail.com
5+
*
6+
* https://github.com/ISKU/Algorithm
7+
* https://www.acmicpc.net/problem/5430
8+
*/
9+
10+
import java.io.*;
11+
import java.util.*;
12+
13+
public class Main {
14+
public static void main(String... args) throws Exception {
15+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
16+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
17+
18+
int T = Integer.parseInt(br.readLine());
19+
while (T-- > 0) {
20+
char[] commands = br.readLine().toCharArray();
21+
int N = Integer.parseInt(br.readLine());
22+
23+
StringTokenizer st = new StringTokenizer(br.readLine(), "[],");
24+
int[] array = new int[N];
25+
for (int i = 0; i < N; i++)
26+
array[i] = Integer.parseInt(st.nextToken());
27+
28+
boolean reverse = false;
29+
boolean error = false;
30+
int front = 0;
31+
int rear = N - 1;
32+
for (char c : commands) {
33+
if (c == 'R')
34+
reverse = !reverse;
35+
if (c == 'D') {
36+
if (reverse)
37+
rear--;
38+
else
39+
front++;
40+
41+
if (rear - front < -1) {
42+
error = true;
43+
break;
44+
}
45+
}
46+
}
47+
48+
if (error) {
49+
bw.write("error\n");
50+
} else {
51+
bw.write('[');
52+
if (reverse) {
53+
if (front <= rear)
54+
bw.write(String.valueOf(array[rear]));
55+
for (int i = rear - 1; i >= front; i--)
56+
bw.write("," + array[i]);
57+
} else {
58+
if (front <= rear)
59+
bw.write(String.valueOf(array[front]));
60+
for (int i = front + 1; i <= rear; i++)
61+
bw.write("," + array[i]);
62+
}
63+
bw.write("]\n");
64+
}
65+
}
66+
67+
bw.close();
68+
}
6269
}

0 commit comments

Comments
 (0)