NYOJ 2 括号配对问题

栈的简单应用。可使用STL。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <stack>
 5 using namespace std;
 6 const int maxn=10000+5;
 7 
 8 char ch[maxn];
 9 stack<char> s;
10 
11 bool deal()
12 {
13     while(!s.empty())
14         s.pop();
15     int len=strlen(ch);
16     for(int i=0;i<len;i++)
17     {
18         if(ch[i]=='('||ch[i]=='[')
19             s.push(ch[i]);
20         else if(!s.empty()&&ch[i]==')')
21         {
22             if(s.top()=='(')
23                 s.pop();
24             else
25                 return 0;
26         }
27         else if(!s.empty()&&ch[i]==']')
28         {
29             if(s.top()=='[')
30                 s.pop();
31             else
32                 return 0;
33         }
34         else
35             return 0;
36     }
37     return s.empty();
38 }
39 
40 int main()
41 {
42     int t;
43     scanf("%d",&t);
44     while(t--)
45     {
46         scanf("%s",ch);
47         if(deal())
48             printf("Yes\n");
49         else
50             printf("No\n");
51     }
52     return 0;
53 }
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-27
  • 2022-12-23
  • 2021-05-19
  • 2021-08-24
  • 2021-05-08
猜你喜欢
  • 2021-07-11
  • 2021-10-14
  • 2022-01-08
  • 2021-05-22
  • 2021-11-04
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案