平衡的括号(名字怪怪的~)

 

题意:

    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


思路:
一个数组,一个栈。3个if,1个else。
从数组里面取括号,是左括号就存到栈里去,因为配对的括号要[]()而不是][和)(,
取到右括号,就判断栈里有没有左括号,有则配对成功,pop出这个括号。
否则将右括号存进去,break,即前面没有可以配对的左括号。输出NO。

源代码:
 1 #include<iostream>
 2 #include<stack>
 3 #include<cstring>  
 4 #include<cstdio>
 5 using namespace std;
 6 int main()
 7 {
 8     int t;
 9     cin >> t;
10     getchar();
11     while (t--)
12     {
13         char c;
14         char s[130];
15         stack<char> sta;                          //定义一个栈
16 
17         cin.getline(s, 130);                      //输入一堆括号
18 
19         int len = strlen(s);
20 
21         for (int i = 0; i < 130 && i < len; i++)         //从数组里面开始取括号
22         {
23             c = s[i];                 
24             if (c == ']')                     // 为右括号
25             {
26                 if (sta.empty() || sta.top() != '[')  //判断队列是否为空,或者存不存在与之配对的左括号
27                 {                                      //为空或者没有与之配对的括号就存进去
28                     sta.push(c);                     
29                     break;
30                 }
31                 else
32                 {
33                     sta.pop();                     //配对成功,则删除队列中的这个括号
34                 }
35             }
36             else if (c == ')')
37             {
38                 if (sta.empty() || sta.top() != '(')
39                 {
40                     sta.push(c);
41                     break;
42                 }
43                 else
44                 {
45                     sta.pop();
46                 }
47             }
48             else if (c == '[' || c == '(')     //取出来的为左括号
49             {                                  //存进去,等待接下来的右括号配对
50                 sta.push(c);
51             }
52             else
53             {
54                 sta.push(c);               //其他字符,直接存进去,break;
55                 break;
56             }
57         }
58         if (sta.empty())                      //队列为空了,已经配对完了。
59         {
60             cout << "Yes" << endl;
61         }
62         else
63         {
64             cout << "No" << endl;
65         }
66 
67     }
68 
69 
70     return 0;
71 }


心得:
 栈,“后进先出”。与队列要进行区分。这道题请教了别人,自己开始的思路还是对的,
但是][跟)(这两种案例都会判成成功。还是要考虑一些特殊情况,栈的用法还是要好好学。
脑子不够用呐(=@__@=)~~~~要有耐心耐心。





------------------------ 没有谁的人生不是斩棘前行 ---------------------------------------- JM
原文地址:https://www.cnblogs.com/Lynn0814/p/4674707.html