1일1알

백준 10799번 쇠막대기 C++ 본문

알고리즘

백준 10799번 쇠막대기 C++

영춘권의달인 2022. 6. 1. 11:19

출처 : https://www.acmicpc.net/problem/10799

() 일 때 : 레이저

(~~) 일 때 : 쇠막대기

 

()가 들어왔을 때 앞에있는 (의 개수만큼 잘린다.

쇠막대기의 끝에서는 한번 더 잘리기 때문에 긴 괄호가 닫힐 때에는 cnt에 1을 더해준다.

 

#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>

using namespace std;
using ll = long long;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	string str;
	cin >> str;
	stack<char> st;
	int cnt = 0;
	bool flag = false;
	for (int i = 0; i < str.length(); i++) {
		if (str[i] == ')') {
			st.pop();
			if (flag) cnt += st.size();
			else cnt++;
			flag = false;
		}
		else {
			flag = true;
			st.push(str[i]);
		}
	}
	cout << cnt;
};

'알고리즘' 카테고리의 다른 글

백준 10815번 숫자 카드 C++  (0) 2022.06.03
백준 1158번 요세푸스 문제 C++  (0) 2022.06.02
백준 17298번 오큰수 C++  (0) 2022.05.31
백준 2573번 빙산 C++  (0) 2022.05.30
백준 2644번 촌수계산 C++  (0) 2022.05.29