[C++] 10799 : 쇠막대기

리폐·2023년 10월 30일

백준

목록 보기
7/18

10799 : 쇠막대기

//10799 쇠막대기 
#include <iostream>
#include <stack>
#include <string>
using namespace std;

// ( ) ( ( ( ( ) ( ) ) ( ( ) ) ( ) | ) ) ( ( ) )
// ( ) << 레이저가 완성될때 스택에 쌓여있는 ( 의 개수 만큼 결과 값에 더해주고 팝
// ) 가 들어 올때 스택를 팝해주고 결과 값에 + 1
//ans += 3 + 3 + 1 + 3 + 1 + 2 + 1 + 1 + 1 + 1

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

	int ans = 0;
	stack<char> stk;
	string st;

	cin >> st;

	for (int i = 0; i < st.length(); i++) {
		if (stk.empty() || st[i] == '(') {
			stk.push(st[i]);
			if (st[i + 1] != st[i]) { // ( 가 들어온 직후 다음 문자열 인덱스 값이 ) 일때
				i++;
				stk.pop();
				if (stk.empty()) continue;
				else ans += stk.size();
			}
		} 
		else if (stk.top() != st[i]) { // 레이저가 아닐때 ) 가 들어올때
			ans += 1;
			stk.pop();
		}
	}

	cout << ans << "\n";
}

profile
Unreal 5, Unity 공부

0개의 댓글