BOJ 3986번 : 좋은 단어 본문
괄호 문제를 풀었다면 바로 스택을 사용하는 문제라는걸 알 수 있다.
어떤 것을 감싸고 있는 집합을 찾아야 하는 경우 스택이 가장 유용하다.
이 문제도 A쌍이 (), B 쌍이 [] 라고 생각하면 편하다.
<전체코드>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
#include<iostream>
#include<string>
#include<algorithm>
#include<cstring>
#include<queue>
#include<stack>
#include<vector>
#include<cmath>
using namespace std;
int main(){
int T;
int ans = 0;
cin>>T;
for(int t = 0; t<T; t++){
stack<char> s;
string tem;
cin>>tem;
s.push(tem[0]);
for(int i = 1; i<tem.size(); i++){
if(!s.empty()){
if(tem[i] == 'A' && s.top() == 'A')
s.pop();
else if(tem[i] == 'B' && s.top() == 'B')
s.pop();
else
s.push(tem[i]);
}
else
s.push(tem[i]);
}
if(s.empty())
ans++;
}
cout<<ans<<endl;
}
|
cs |
'BOJ' 카테고리의 다른 글
BOJ 13975번 : 파일 합치기3 (0) | 2022.03.26 |
---|---|
BOJ 18352번 : 특정 거리의 도시 찾기 (0) | 2022.03.18 |
BOJ 16120번 : PPAP (0) | 2022.03.18 |
BOJ 1012 : 유기농 배추 (0) | 2022.03.07 |
BOJ 15651번 : N과 M(3) (0) | 2020.11.16 |
Comments