-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7-3 括号匹配 (25 分).cpp
56 lines (54 loc) · 970 Bytes
/
7-3 括号匹配 (25 分).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
54
55
56
#include<bits/stdc++.h>
using namespace std;
stack<char> s;
map<char,int> ma;
map<char,int> ma2;
int main(){
ma['(']=ma[')']=1;
ma['[']=ma[']']=2;
ma['{']=ma['}']=3;
ma2['(']=ma2['{']=ma2['[']=1;
ma2[')']=ma2[']']=ma2['}']=2;
string str;
// cin>>str;
getline(cin,str);
// str="{[}]";
//cout<<"str="<<str<<endl;
int len=str.length();
char top;
for(int i=0;i<len;++i){
if(str[i]=='('||str[i]==')'||str[i]=='{'||str[i]=='}'||str[i]=='['||str[i]==']'){
if(s.empty()){
s.push(str[i]);
continue;
}
top=s.top();
if(ma2[top]==ma2[str[i]]){
s.push(str[i]);
}
else if(ma[top]!=ma[str[i]]){
cout<<"no"<<endl;
return 0;
}else if(ma[top]==ma[str[i]]&&top!=str[i]){
s.pop();
}
}
}
char t;
while(!s.empty()){
top=s.top();
s.pop();
if(s.empty()){
cout<<"no"<<endl;
return 0;
}
t=s.top();
s.pop();
if(ma[top]!=ma[t]){
cout<<"no"<<endl;
return 0;
}
}
cout<<"yes"<<endl;
return 0;
}