首页 > 其他 > 详细

1355:字符串匹配问题(strs)

时间:2021-08-25 18:40:16      阅读:24      评论:0      收藏:0      [点我收藏+]

字符串匹配问题

使用全局stack时,切记清空stack!

 1 #include<iostream>
 2 #include<cstring>
 3 #include<stack>
 4 using namespace std;
 5 
 6 stack<int> sta;
 7 
 8 bool match(){
 9     string s;
10     cin>>s;
11     for(int i=0,t;i<s.length();i++){
12         t=0;
13         if(s[i]==<)t=1;if(s[i]==()t=2;
14         if(s[i]==[)t=3;if(s[i]=={)t=4;
15         if(s[i]==})t=5;if(s[i]==])t=6;
16         if(s[i]==))t=7;if(s[i]==>)t=8;
17         if(t>0&&t<5){
18             if(sta.empty()||sta.top()>=t)sta.push(t);
19             else return 0;
20         }
21         if(t>4&&t<9){
22             if(!sta.empty()&&sta.top()+t==9)sta.pop();
23             else return 0;
24         }
25     }
26     if(sta.empty())return 1;
27     else return 0;
28 }
29 int main(){
30     int n;
31     cin>>n;
32     while(n--){
33         if(match())cout<<"YES\n";
34         else cout<<"NO\n";
35         while(!sta.empty())sta.pop();//清空sta
36     }
37     return 0;
38 }

 

1355:字符串匹配问题(strs)

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!