PS

BOJ 3986 : 좋은 단어

lickelon 2024. 12. 31. 23:12
  • 문제 링크 : boj.kr/3986
  • 난이도 : S4
  • 태그 : 스택

코드

#include <bits/stdc++.h>

#define all(x) (x).begin(), (x).end()

#define INF 0x7FFFFFFF

using namespace std;

using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll, ll>;

bool check(string s) {
    stack<char> _st;
    for(auto e : s) {
        if(!_st.empty() && _st.top() == e) _st.pop();
        else _st.push(e);
    }
    return _st.empty();
}

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

    int n;
    cin >> n;
    int ans = 0;
    for(int i = 0; i < n; i++) {
        string s;
        cin >> s;
        if(check(s)) ans++;
    }
    cout << ans;

    return 0;
}

풀이

괄호 문자열 판별과 비슷하다.

스택을 이용하여 문제를 해결할 수 있다.

728x90

'PS' 카테고리의 다른 글

BOJ 28136 : 원, 탁!  (0) 2025.01.02
BOJ 11722 : 가장 긴 감소하는 부분 수열  (0) 2025.01.02
BOJ 10816 : 숫자 카드 2  (0) 2024.12.30
BOJ 33049 : 마작에서 가장 어려운 것  (0) 2024.12.30
BOJ 17211 : 좋은 날 싫은 날  (0) 2024.12.28