NYOJ2 括号配对问题

 

括号配对问题

时间限制:3000 ms  |  内存限制:65535 KB
难度:3
 
描述
现在,有一行括号序列,请你检查这行括号是否配对。
 
输入
第一行输入一个数N(0<N<=100),表示有N组测试数据。后面的N行输入多组输入数据,每组输入数据都是一个字符串S(S的长度小于10000,且S不是空串),测试数据组数少于5组。数据保证S中只含有"[","]","(",")"四种字符
输出
每组输入数据的输出占一行,如果该字符串中所含的括号是配对的,则输出Yes,如果不配对则输出No
样例输入
3
[(])
(])
([[]()])
样例输出
No
No
Yes
View Code
 1 用C++稍微慢了一点
 2 /*
 3 #include<iostream>
 4 #include<string>
 5 int main()
 6 {
 7     using namespace std;
 8     int n;
 9     string s;
10     cin>>n;
11     while(n--)
12     {
13         cin>>s;
14         int len=s.size();
15         int top=0;
16         for(int i=1;i<len;++i)
17         {
18             if(s[i]==']'&&s[top]=='['||s[i]==')'&&s[top]=='(')
19                 --top;
20             else 
21             {
22                 ++top;
23                 s[top]=s[i];
24             }
25         }
26         if(top==-1)
27             cout<<"Yes"<<endl;
28         else
29             cout<<"No"<<endl;
30     }
31     return 0;
32 }
33 */
34 #include<stdio.h>
35 #include<string.h>
36 int main()
37 {
38     int n,len,top,i;
39     char s[10001];
40     scanf("%d",&n);
41     getchar();
42     while(n--)
43     {
44         gets(s);
45         len=strlen(s);
46         top=0;
47         for(i=1;i<len;++i)
48         {
49             if(s[i]==']'&&s[top]=='['||s[i]==')'&&s[top]=='(')
50                 --top;
51             else 
52             {
53                 ++top;
54                 s[top]=s[i];
55             }
56         }
57         if(top==-1)
58             printf("Yes\n");
59         else
60             printf("No\n");
61     }
62     return 0;
63 }
功不成,身已退
原文地址:https://www.cnblogs.com/dongsheng/p/2571500.html