본문 바로가기
카테고리 없음

[백준] 3986. 좋은 단어(c++)

by backend 개발자 지망생 2025. 2. 11.

https://www.acmicpc.net/problem/3986

중간에 빠져나와야 한다는 생각에 매몰되었었다.

교차한다는 것이 무엇일까??를 정의하는 것이 중요했다.


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <limits.h>
#include <queue>
#include <set>
#include <math.h>
#include <stack>
#include <deque>

using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n, ans = 0;

    cin >> n;

    while (n--) {
        string s;
        stack<char> stk;

        cin >> s;

        for (char i: s) {
            if (stk.empty())
                stk.push(i);
            else {
                if (stk.top() == i) {
                    stk.pop();
                } else {
                    stk.push(i);
                }
            }
        }

        if (stk.empty()) {
            ans++;
        }
    }

    cout << ans << "\n";

    return 0;
}