模拟棧的操作,很基礎的一道題
題目連結: http://acm.nyist.net/JudgeOnline/problem.php?pid=2
1
2 #include<stdio.h>
3 #include<stdlib.h>
4 #include<string.h>
5 char a[11000],b[11000];//b即stack;
6 int main()
7 {
8 int n;
9 scanf("%d",&n);
10 getchar();//屏蔽回車;
11 while(n--)
12 {
13 int len,top=1,i;
14 gets(a);
15 b[top++]=a[0];
16 len=strlen(a);
17 if((a[0]!='[')&&(a[0]!='(')||(len%2==1)) printf("No\n");//若第一個元素為")"或"]"輸入個數為奇數,No;
18 else
19 {
20 for(i=1; i<len; i++)
21 {
22 if(a[i]=='('||a[i]=='[') b[top++]=a[i];//滿足進棧條件,進棧;
23 else
24 {
25 if(a[i]==']'&&b[top-1]=='[') top--;//滿足出棧條件;
26 else if(a[i]==')'&&b[top-1]=='(') top--;
27 else b[top++]=a[i];
28 }
29 }
30 if(top==1) printf("Yes\n");//如果最後top回到原位( 都配對 );
31 else printf("No\n");
32 }
33 }
34 return 0;
35 }
View Code
轉載于:https://www.cnblogs.com/Just--Do--It/p/6027135.html