BOJ

3986 좋은 단어

공부정리 2018. 2. 17. 23:35

괄호 문제와 동일하다.


스택의 top에 있는것과 같으면 스택을 pop


아니면 push


다 끝나고 비어있으면 좋은단어


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
#include <stdio.h>
#include <stack>
#include <string.h>
using namespace std;
 
int n, ans;
char t[100002];
 
int main() {
    scanf("%d"&n);
    while (n--) {
        scanf(" %s"&t);
        stack<char> s;
        for (int i = 0; t[i] == 'A' || t[i] == 'B'; i++) {
            if (s.empty()) s.push(t[i]);
            else {
                if (s.top() == t[i]) s.pop();
                else s.push(t[i]);
            }
        }
        if (s.empty()) ans++;
    }
    printf("%d\n", ans);
    return 0;
}
cs