20250812 baekjoon

This commit is contained in:
songyc macbook 2025-08-12 22:21:22 +09:00
parent 2cafb89098
commit 23a580cc90

View File

@ -0,0 +1,58 @@
import java.util.*;
public class _5430 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = Integer.parseInt(sc.nextLine());
for(int t = 0; t<T; t++){
boolean invertFlag = false;
boolean errorFlag = false;
String opcode = sc.nextLine();
int n = Integer.parseInt(sc.nextLine());
String[] line = sc.nextLine().replace("[","").replace("]","").split(",");
Deque<Integer> arr = new LinkedList<>();
for(String s : line){
if(s.isEmpty()) break;
arr.add(Integer.parseInt(s));
}
for(int i=0; i<opcode.length(); i++){
if(opcode.charAt(i) == 'R'){
invertFlag = !invertFlag;
}
else if(opcode.charAt(i) == 'D'){
n--;
Integer temp;
if(invertFlag){
temp = arr.pollLast();
}
else {
temp = arr.pollFirst();
}
if(temp == null){
errorFlag = true;
break;
}
}
}
if(errorFlag){
System.out.println("error");
continue;
}
StringBuilder sb = new StringBuilder("[");
for(int ii=0; ii<n; ii++){
if(invertFlag) sb.append(arr.pollLast());
else sb.append(arr.pollFirst());
if(ii!=n-1) sb.append(",");
}
sb.append("]");
System.out.println(sb.toString());
}
sc.close();
}
}