Little Jay

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

알고리즘/BOJ

[C++] 백준 10799번 쇠막대기

Jay, Lee 2021. 8. 2. 16:20

이렇게 푸는 것이 맞는지는 모르겠지만 풀이를 설명하자면 

먼저 ( 가 들어올 때 스택에다가 push를 해준다

만약 ( 가 들어오고 다음게 ) 면 레이저를 쏴주야 하기 때문에 

스택의 사이즈만큼 계속 더해준다

만약 그것도 아니고 ) 만 들어온다면 마지막에는 하나씩 남는 막대가 생기기 때문에

하나씩 total 개수에 더해준다

 

#include <iostream>
#include <string>
#include <stack>
using namespace std;


int main() {

	string s;
	cin >> s;
	stack<char> st;

	int total_bar = 0;

	for (int i = 0; i < s.size(); i++) {
		if (s[i] == '(' && s[i + 1] == ')') {
			total_bar += st.size();
			i++;
			continue;
		}
		if (s[i] == '(') {
			st.push(s[i]);
		}
		else {
			total_bar++;
			st.pop();
		}
	}

	cout << total_bar << "\n";

	return 0;
}
Comments