C++)
#include <iostream>
#include <stack>
using namespace std;
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
int ans = 0;
string temp;
while (n--) {
cin >> temp;
stack<char> st;
bool flag = false;
for (auto ele : temp) {
if (!st.empty() && st.top() == ele)
st.pop();
else
st.push(ele);
}
if (st.empty())
ans++;
}
cout << ans;
return 0;
}
Python)
import sys
read = sys.stdin.readline
n = int(read().rstrip())
ans = 0
for _ in range(n):
temp = read().rstrip()
st = []
for x in temp:
if st and st[-1] == x:
st.pop()
else:
st.append(x)
ans += (1 if not st else 0)
print(ans)'백준 1 > 자료구조' 카테고리의 다른 글
| [백준 1406] 에디터 (Python) (0) | 2021.01.11 |
|---|---|
| [백준 2841] 외계인의 기타 연주 (C++/Java) (0) | 2020.12.06 |
| [백준 1935] 후위 표기식2 (C++/Java) (0) | 2020.12.06 |
| [백준 1918] 후위 표기식 (C++/Java) (0) | 2020.12.06 |
| [백준 1874] 스택 수열 (C++/Python) (0) | 2020.12.06 |