컴퓨터 공학/백준
[백준] JAVA 자바 : 단어 뒤집기 2 (17413번)
kim-dev
2024. 1. 20. 11:48
반응형

앞에서 만들었던 단어 뒤집기의 심화 버전이다.
사실 앞에서 만들었던 단어 뒤집기는 스택만 쓰면 공백만 잘 처리해주면 쉽게 구현할 수 있었다.
이번 경우는 <>을 사용하는 주석이 추가돼서 저 주석은 그대로 출력해줘야 한다.
그래서… 이번에는 큐를 사용해야 할 듯!
import java.io.*;
import java.util.Stack;
import java.util.Queue;
import java.util.LinkedList;
public class ReverseWord2_17413 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
Stack<Integer> stack = new Stack<>();
Queue<Integer> queue = new LinkedList<>();
String str = br.readLine();
for (int i=0; i<str.length(); i++) {
if (str.charAt(i) == '<') { // '<'를 만나면 그대로 출력해야 함
while (!stack.isEmpty()) { // 그 전에 스택에 문자가 넣어져 있다면
int tmp_int = stack.pop(); // 다 출력해주고 시작한다.
char tmp_c = (char)tmp_int;
bw.write(tmp_c);
}
int j = i; // '<' 부터
while (str.charAt(j) != '>') { // '>' 앞까지
queue.offer((int)str.charAt(j)); // 다 큐에 넣는다
j++;
}
queue.offer((int)str.charAt(j)); // '>'까지 큐에 넣음
while (queue.size() != 0) { // 큐의 요소를 전부 빼서 출력한다
int tmp_int = queue.poll(); // 어차피 FIFO니까 그대로 출력됨
char tmp_c = (char)tmp_int;
bw.write(tmp_c);
}
i = j; // '<' ~ '>'까지는 건너뛰어줌
continue;
}
if (str.charAt(i) == ' ') { // 해당 문제가 공백일 경우
while (!stack.isEmpty()) { // 앞에서 스택에 담았던 문자들을 출력
int tmp_int = stack.pop(); // 스택은 LIFO니까 거꾸로 출력됨
char tmp_c = (char)tmp_int;
bw.write(tmp_c);
}
bw.write(" ");
} else {
stack.push((int)str.charAt(i)); // 공백이 아니면 스택에 담는다
}
}
while (!stack.isEmpty()) { // for문을 다 돌면
int tmp_int = stack.pop(); // 스택에 남은 문자들을 다 출력한다
char tmp_c = (char)tmp_int;
bw.write(tmp_c);
}
bw.close();
br.close();
}
}
근데 사실… 나는 이렇게 짜긴 했는데 이렇게 하면 반복문이 너무 많이 나와서 보기에도 안 좋고 오히려 시간이 오래 걸릴 수도…?
그런데 제출 기록 보니 내가 짠 게 은근 빠른 편인 거 같기도 하넹 ㅋㅋㅋ
로그인
www.acmicpc.net
작성일자: 2023-09-03