[BOJ] 10799번 쇠막대기

yeham·2022년 11월 4일
0

백준

목록 보기
1/22

문제

쇠막대기

코드

#include <iostream>
#include <vector>
#include <list>
#include <queue>
#include <stack>

using namespace std;

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);

	stack<int> st;
	string s;
	getline(cin, s);
	int i = 0;
	int count = 0;
	int total = 0;
	int temp;

	while (s[i])
	{
		if (s[i] == '(' && s[i+1] != ')')
		{
			st.push(s[i]);
		}
		else if (s[i] == '(' && s[i+1] == ')' && !st.empty())
		{
			total += st.size();
			i++;
		}
		else if (!st.empty() && s[i] == ')' &&  st.top() == '(')
		{
			st.pop();
			total++;
		}
		i++;
	}
	cout << total;
	return (0);
}
profile
정통과 / 정처기 & 정통기 / 42seoul 7기 Cardet / 임베디드 SW 개발자

0개의 댓글