首页 > 其他 > 详细

UVa 673 Parentheses Balance

时间:2014-01-31 14:21:39      阅读:414      评论:0      收藏:0      [点我收藏+]
  Parentheses Balance 

You are given a string consisting of parentheses () and []. A string of this type is said to be correct:

(a)
if it is the empty string
(b)
if A and B are correct, AB is correct,
(c)
if A is correct, (A) and [A] is correct.

Write a program that takes a sequence of strings of this type and check their correctness. Your program can assume that the maximum string length is 128.

 

Input 

The file contains a positive integer n and a sequence of n strings of parentheses () and [], one string a line.

 

Output 

A sequence of Yes or No on the output file.

 

Sample Input 

 

3
([])
(([()])))
([()[]()])()

 

Sample Output 

Yes
No
Yes

 

 


Miguel Revilla 
2000-08-14

 

括号匹配问题,就是摸拟一个栈的操作:‘(’和‘[’相当于入栈操作,‘)’和‘]’相当于出栈操作

注意处理完一行之后栈必需为空才能输出Yes

 

bubuko.com,布布扣
 1 #include<iostream>
 2 #include<cstdio>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int kase;
 9 
10     scanf("%d",&kase);
11     getchar();
12 
13     while(kase--)
14     {
15         int f=0;
16         char s[150],Stack[150];
17         bool finished=false,yes=true;
18 
19         gets(s);
20 
21         for(int i=0;(!finished)&&s[i]!=\0;i++)
22         {
23             switch(s[i])
24             {
25                 case (:Stack[f++]=(;break;
26                 case [:Stack[f++]=[;break;
27                 case ):if(f>0&&Stack[f-1]==()
28                             f--;
29                          else
30                          {
31                              finished=true;
32                              yes=false;
33                          }
34                          break;
35                 case ]:if(f>0&&Stack[f-1]==[)
36                             f--;
37                          else
38                          {
39                              finished=true;
40                              yes=false;
41                          }
42                          break;
43             }
44         }
45 
46         printf("%s\n",yes&&(f==0)?"Yes":"No");
47     }
48 
49     return 0;
50 }
[C++]

UVa 673 Parentheses Balance

原文:http://www.cnblogs.com/lzj-0218/p/3536729.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!