首页 > 代码库 > 算法竞赛入门经典(第六章)

算法竞赛入门经典(第六章)

习题6-1,UVa673,Time:11.1

 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cstdlib> 5 #include<string> 6 #include <stack> 7 using namespace std; 8 int main() { 9     int Case;10     string str;11     bool flag;12     scanf("%d",&Case);13     for(int c = 1; c <= Case; c++)14     {15         stack<char>q;16         flag = true;17         cin>>str;18         for(int i = 0; i < str.length(); i++)19         {20             if(q.size() == 0 || str[i]==( || str[i]==[)        q.push(str[i]);21             else22             {23                 if((str[i]==) && q.top()==() || (str[i]==] && q.top()==[))24                 {25                     q.pop();26                 }27                 else28                 {29                     flag = false;30                     break;    31                 }32             }33         }34         if(flag==false || q.size())        cout<<"NO"<<endl;35         else     cout<<"YES"<<endl;36     }37     return 0;38 }
View Code

 

算法竞赛入门经典(第六章)