3986번: 좋은 단어
이번 계절학기에 심리학 개론을 수강 중인 평석이는 오늘 자정까지 보고서를 제출해야 한다. 보고서 작성이 너무 지루했던 평석이는 노트북에 엎드려서 꾸벅꾸벅 졸다가 제출 마감 1시간 전에
www.acmicpc.net
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int goodWord = 0;
Stack<Character> stack = new Stack<>();
for (int i = 0; i < n; i++) {
String word = br.readLine();
for (int j = 0; j < word.length(); j++) {
if (!stack.isEmpty() && stack.peek().equals(word.charAt(j))) {
stack.pop();
} else {
stack.push(word.charAt(j));
}
}
if (stack.isEmpty()) {
goodWord += 1;
} else {
stack.clear();
}
}
System.out.println(goodWord);
}
}
이 문제는 나중에 다시 풀어 봐야함.
Stack을 사용하면 편하구나 라는 생각이 들지 않았었음.
단지 알고리즘 유형이 Stack이라 Stack으로 푼 것.
나중에 이 문제를 다시 봤을 때 Stack으로 풀면 효율적이겠다 라는 생각이 들어야 함
'Java > AlgorithmPS' 카테고리의 다른 글
백준) 도키도키 간식드리미 (12789번) (0) | 2023.09.27 |
---|---|
백준) 괄호 (9012번) (0) | 2023.09.26 |
백준) 단어 뒤집기2(17413번) (0) | 2023.09.23 |
백준) 과제는 끝나지 않아(17952번) (0) | 2023.09.23 |
백준) 스택 수열(1874번) (0) | 2023.09.22 |