返回列表 發帖
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. string str;
  4. int main()
  5. {
  6.     while(cin>>str)
  7.     {
  8.         bool m=true;
  9.         stack<char> sp;
  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(sp.empty())
  17.                 {
  18.                     m=false;
  19.                     break;
  20.                 }
  21.                 if(str[i]-sp.top()==1||str[i]-sp.top()==2)
  22.                 {
  23.                     sp.pop();
  24.                 }else{
  25.                     m=false;
  26.                     break;
  27.                 }
  28.             }
  29.         }
  30.         if(m==true&&sp.empty())
  31.         {
  32.         cout<<"yes\n";
  33.         }else{
  34.         cout<<"no\n";
  35.         }
  36.     }
  37. }
複製代碼

TOP

返回列表