-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntegerList.java
51 lines (48 loc) · 1.82 KB
/
IntegerList.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
public class IntegerList {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(System.out);
int c = Integer.parseInt(bf.readLine());
for(int i = 0; i < c; i++) {
char[] command = bf.readLine().toCharArray();
int numOfElement = Integer.parseInt(bf.readLine());
String input = bf.readLine().replaceAll("\\[", "").replaceAll("\\]","");
String[] inputs = input.split(",");
ArrayList<Integer> lst = new ArrayList<>();
for(int j = 0; j < numOfElement; j++) {
lst.add(Integer.parseInt(inputs[j]));
}
for(int k = 0; k < command.length; k++){
char com = command[k];
if(com == 'R'){
Collections.reverse(lst);
} else if(com == 'D') {
if(lst.isEmpty()) {
writer.write("error");
} else {
lst.remove(0);
}
}
}
for(int x = 0; x < lst.size(); x++){
if(x == 0) {
writer.write("[" + lst.get(x) + ",");
} else if( x == lst.size() - 1) {
writer.write(lst.get(x) + "]");
} else {
writer.write(lst.get(x) + ",");
}
}
writer.println();
}
bf.close();
writer.flush();
writer.close();
}
}