[BOJ 17413] 단어 뒤집기2 (java)
문제 링크 https://www.acmicpc.net/problem/17413
문제 풀이
package basics_200;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
public class BOJ17413 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Stack<Character> stack = new Stack<Character>();
StringBuilder sb = new StringBuilder();
String s = br.readLine() + "\n";
boolean open = false;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '<') {
while (!stack.isEmpty()) {
sb.append(stack.pop());
}
sb.append(s.charAt(i));
open = true;
} else if (s.charAt(i) == '>') {
sb.append(s.charAt(i));
open = false;
} else {
if (open == true) {
sb.append(s.charAt(i));
} else {
if (s.charAt(i) == ' ' || s.charAt(i) == '\n') {
while (!stack.isEmpty()) {
sb.append(stack.pop());
}
if (s.charAt(i) == ' ') {
sb.append(' ');
}
} else {
stack.push(s.charAt(i));
}
}
}
}
System.out.println(sb);
}
}
Comments