返回列表 發帖
  1. #include<bits/stdc++.h>
  2. using namespace std;

  3. int main()
  4. {
  5.     string str;
  6.     while(cin>>str)
  7.     {
  8.         stack<char> sp;
  9.         bool match=true;
  10.         for(int i=0;i<str.length();i++)
  11.         {
  12.             if(str[i]=='(' || str[i]=='[' ||str[i]=='{')
  13.                 sp.push(str[i]);
  14.             if(str[i]==')' || str[i]==']' ||str[i]=='}')
  15.             {
  16.                 if(str.length()%2==1)
  17.                 {
  18.                     match=false;
  19.                     break;
  20.                 }
  21.                 if(sp.empty())
  22.                 {
  23.                     match=false;
  24.                     break;
  25.                 }
  26.                 if(str[i]-sp.top()==1 || str[i]-sp.top()==2)
  27.                 {
  28.                     sp.pop();
  29.                 }
  30.                 else
  31.                 {
  32.                     match=false;
  33.                     break;
  34.                 }
  35.             }
  36.         }
  37.         if(match and sp.empty())
  38.             cout<<"yes"<<endl;
  39.         else
  40.             cout<<"no"<<endl;
  41.     }
  42.     return 0;
  43. }
複製代碼

TOP

返回列表