문제: 3986번: 좋은 단어
basic-algo-lecture/workbook/0x08.md at master · encrypted-def/basic-algo-lecture
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip> // setprecision을 사용하기 위한 헤더
#include <climits>
#include <list>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
int cnt = 0;
for (int i = 0; i < n; i++) {
string word;
cin >> word;
stack<char> s;
for (int j = 0; j < word.size(); j++) {
if (s.empty() || s.top() != word[j])
s.push(word[j]);
else if (s.top() == word[j])
s.pop();
}
if (s.empty())
cnt++;
}
cout << cnt << "\n";
return 0;
}
참고자료:
BaaaaaaaarkingDog | [실전 알고리즘] 0x08강 - 스택의 활용(수식의 괄호 쌍)