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