https://www.acmicpc.net/problem/5397
public class BOJ5397 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
Stack<Character> preOrder = new Stack<>();
Stack<Character> nextOrder = new Stack<>();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < N; i++) {
String words = br.readLine();
for (int j = 0; j < words.length(); j++) {
Character c = words.charAt(j);
if (c == '-') {
if (!preOrder.isEmpty()) {
preOrder.pop();
}
} else if (c == '<') {
if (!preOrder.isEmpty()) {
nextOrder.push(preOrder.pop());
}
} else if (c == '>') {
if (!nextOrder.isEmpty()) {
preOrder.push(nextOrder.pop());
}
} else {
preOrder.push(c);
}
//백스페이스
// <<
// >>
}
while (!preOrder.isEmpty()) {
nextOrder.push(preOrder.pop());
}
while (!nextOrder.isEmpty()) {
sb.append(nextOrder.pop());
}
sb.append("\n");
}
System.out.println(sb.toString());
}
}
https://velog.io/@hoha/BOJ%EB%B0%B1%EC%A4%805397%EB%B2%88-%ED%82%A4%EB%A1%9C%EA%B1%B0JAVA