返回列表 發帖
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main() {
  4.     string a;
  5.     while(cin>>a)
  6.     {
  7.         stack<char> st;
  8.         bool ma=true;
  9.         for(int i=0;i<a.length();i++)
  10.         {
  11.             if(a[i]=='(' or a[i]=='[' or a[i]=='{')
  12.                 st.push(a[i]);
  13.             if(a[i]==')' or a[i]==']' or a[i]=='}')
  14.             {
  15.                 if(st.empty())
  16.                 {
  17.                     ma=false;
  18.                     break;
  19.                 }else if(a[i]-st.top()==1 or a[i]-st.top()==2)
  20.                 {
  21.                     st.pop();
  22.                 }else
  23.                 {
  24.                     ma=false;
  25.                     break;
  26.                 }
  27.             }   
  28.         }
  29.         cout<<((st.empty() && ma==true) ? "yes\n" : "no\n");
  30.     }
  31.     return 0;
  32. }
複製代碼

TOP

返回列表