-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4889_안정적인문자열.cpp
56 lines (51 loc) · 1.2 KB
/
4889_안정적인문자열.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <cstdio>
#include <vector>
#include <algorithm>
#include <iostream>
#include <string>
#include <bits/stdc++.h>
#include <sstream>
#include <stack>
#include <queue>
using namespace std;
stack<int>stk;
string str;
int main() {
int loof = 1;
int check = 0;
while (1) {
int res = 0;
char c = '-';
cin >> str;
if (str.find(c) != string::npos) { //문자열중 문자가 포함되는지 여부확인
break;
}
for (int i = 0; i < str.length(); i++) {
if (str[i] == '{') {
stk.push(str[i]);
}
else {
if (!stk.empty() && stk.top() == '{') { //괄호가 정상적으로 닫힐 수 있는 경우 stack에서 제거
stk.pop();
}
else { //정상적이지 않을 때는 변경해서 넣어줌
res++;
stk.push('{');
}
}
}
if (stk.empty()) { // 변경을 통해 괄호가 정상적으로 닫혀서 스택이 비면 변경횟수만 출력
cout << "빈스택\n";
cout << loof << ". " << res<<"\n";
}
else {
cout << loof << ". " << stk.size()/2+res << "\n";
//변경을 해도 스택이 비지 않았을 때는 변경횟수 + stack의 사이즈를 2로 나눈값 출력
while (!stk.empty()) {
stk.pop();
}
}
loof++;
}
return 0;
}