1355:字符串匹配問題(strs)


字符串匹配問題

使用全局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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM