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

TOP

返回列表