1354:括弧匹配检验

时间:2021-08-25 12:41:22   收藏:0   阅读:6

括弧匹配检验

 1 #include<iostream>
 2 #include<cstring>
 3 #include<stack>
 4 using namespace std;
 5 
 6 stack<char> sta;
 7 
 8 bool match(){
 9     string s;
10     cin>>s;
11     for(int i=0;i<s.length();i++){
12         if(s[i]==‘(‘||s[i]==‘[‘)sta.push(s[i]);
13         else if(s[i]==‘)‘){
14             if(!sta.empty()&&sta.top()==‘(‘)sta.pop();
15             else return 0;
16         }else{
17             if(!sta.empty()&&sta.top()==‘[‘)sta.pop();
18             else return 0;
19         }
20     }
21     if(sta.empty())return 1;
22     else return 0;
23 }
24 int main(){
25     if(match())cout<<"OK";
26     else cout<<"Wrong";
27     return 0;
28 }

 

原文:https://www.cnblogs.com/sxrekord/p/bracket_matching_test.html

评论(0)
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!