백준 3986

HR·2022년 4월 9일
0

알고리즘 문제풀이

목록 보기
12/50

백준 3986 : 좋은 단어

  1. stack 사용

정답 코드

#include <iostream> 
#include <stack>

using namespace std;

int n, cnt;
string s;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	
	cin>>n;
	
	for(int i=0; i<n; i++) {
		stack<int> stk;
		
		cin>>s;

		stk.push(s[0]);
		for(int j=1; j<s.length(); j++) {
			if(stk.empty()) {
				stk.push(s[j]);
			}
			else if(char(stk.top())==s[j]) {
				stk.pop();
			}
			else {
				stk.push(s[j]);
			}
		}

		if(stk.empty()) {
			cnt++;
		}		
	}
	
	cout<<cnt<<'\n';
	
	return 0;
}

0개의 댓글