본문 바로가기

algorithm

백준 3986번 좋은 단어

https://www.acmicpc.net/problem/3986

 

아이디어:

1. string tmp로 입력받고 for (auto x : tmp)로 순회한다.

2. stack<char>

3. 스택이 비어있지않고 x와 top()이 같은 글자라면 pop()

4. 그렇지 않다면 push()

5. 마지막에 스택이 비어있으면 ans++

 

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main() {

	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	int N, ans = 0;
	cin >> N;

	while (N--) {
		string tmp;
		stack<char> S;
		cin >> tmp;

		for (auto x : tmp) {
			if (!S.empty() && S.top() == x) S.pop();
			else S.push(x);
		}
		if (S.empty()) ans++;
	}

	cout << ans;

	return 0;

}

'algorithm' 카테고리의 다른 글

백준 1926번 그림  (0) 2025.04.05
넓이우선탐색 BFS  (0) 2025.04.05
백준 4949번 균형잡힌 세상  (0) 2025.04.03
백준 11003번 최솟값 찾기 - 덱 정렬  (0) 2025.04.01
백준 5430번 AC  (0) 2025.03.30