返回列表 發帖
  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.             {
  14.                 sp.push(str[i]);

  15.             }
  16.             if(str[i] ==')' || str[i] ==']' || str[i] =='}')
  17.             {
  18.                 if(sp.size()==0)
  19.                 {
  20.                     match=false;
  21.                     break;
  22.                 }
  23.                 if(str[i]-sp.top()==2 || str[i]-sp.top()==1)
  24.                 {
  25.                     sp.pop();
  26.                 }

  27.             }
  28.         }
  29.         if(match==true && sp.size()==0)
  30.         {
  31.             cout<<"yes\n";
  32.         }
  33.         else
  34.         {
  35.             cout<<"no\n";
  36.         }
  37.     }
  38. }
複製代碼
    ⪔〠   

TOP

返回列表