알고리즘
백준 10799번 쇠막대기 C++
영춘권의달인
2022. 6. 1. 11:19
() 일 때 : 레이저
(~~) 일 때 : 쇠막대기
()가 들어왔을 때 앞에있는 (의 개수만큼 잘린다.
쇠막대기의 끝에서는 한번 더 잘리기 때문에 긴 괄호가 닫힐 때에는 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;
};