[BOJ] 4949 균형잡힌 세상

Time Lapse :NONE

4949.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
#include <stack>
#include <algorithm>
#include <iostream>
using namespace std;
int main(void){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
const string opening("(["), closing(")]");
while(1){
string str;
getline(cin,str);
if(str.length()==1&&str[0]=='.'){
return 0;
}
bool flag = true;
stack<char> s;

for(int i=0;i<str.length();i++){
if(str[i]=='('||str[i]=='['||str[i]==')'||str[i]==']'){
if(opening.find(str[i])!=-1){
s.push(str[i]);
}
else{
if(s.empty()){
flag = false;
break;
}

if(opening.find(s.top())!=closing.find(str[i])){
flag = false;
break;
}

s.pop();
}
}
}
if(!flag||!s.empty())
cout<<"no"<<endl;
else
cout<<"yes"<<endl;
}
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/4949/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.