UVA 673 Parentheses Balance 解题心得

原题

Description

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, () and [] 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

分析:简单题,直接用栈就可以

我的代码:

 1 #include<iostream>
 2 #include<stack>
 3 #include<cstring>
 4 #include<cstdio>
 5 using namespace std;
 6
 7 char s[150];
 8
 9 bool ifpop(const char a, const char b)
10 {
11     if (a == ‘(‘&&b == ‘)‘)            return 1;
12     if (a == ‘[‘&&b == ‘]‘)            return 1;
13     return 0;
14 }
15
16
17 int main()
18 {
19     int t;
20     cin >> t;
21     getchar();
22     while (t--)
23     {
24         stack<char> z;
25         gets(s);
26         if (s[0] == ‘\0‘)
27         {
28             puts("Yes");
29             continue;
30         }
31         else
32         {
33             for (int i = 0;s[i]!=‘\0‘; i++)
34             {
35                 if (z.empty())
36                 {
37                     z.push(s[i]);
38                 }
39                 else
40                 {
41                     if (ifpop(z.top(), s[i]))
42                     {
43                         z.pop();
44                     }
45                     else
46                     {
47                         z.push(s[i]);
48                     }
49                 }
50             }
51             if (z.empty())                puts("Yes");
52             else                        puts("No");
53         }
54
55
56     }
57     return 0;
58 }
时间: 2024-10-10 02:43:17

UVA 673 Parentheses Balance 解题心得的相关文章

UVA - 673 - Parentheses Balance (栈的应用!)

UVA - 673 Parentheses Balance Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description  Parentheses Balance  You are given a string consisting of parentheses () and []. A string of this type is said to be co

uva 673 Parentheses Balance

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 pr

UVa 673 Parentheses Balance(括号配对 栈)

题意  判断输入的括号序列是否是配对的 栈的基础应用  栈顶元素与输入的字符匹配就出栈咯  注意括号序列可以为空 STL栈 #include <bits/stdc++.h> using namespace std; int main() { int cas; char c; cin >> cas; getchar(); while(cas--) { stack<char> s; while((c = getchar()) != '\n') { if(s.empty())

UVa 673 Parentheses Balance(栈的使用)

 栈 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description 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, A

UVa 673 Parentheses Balance【栈】

题意:输入一个包含"()"和"[]"的序列,判断是否合法 用栈来模拟,遇到"(",“[”就入栈,遇到')',']'就取出栈顶元素看是否匹配,如果不匹配,则不合法 还有注意一下每次取出栈顶元素的时候判断栈是否为空,如果为空就要跳出循环 注意空串也是合法的串 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmat

UVA 637 Parentheses Balance(栈)

题目代号:HDU 1237 题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_problem&problem=614 题目原文: Parentheses Balance You are given a string consisting of parentheses () and []. A string of this type

UVa 1647 - Computer Transformation 解题心得

这个题目.... 想上题意 10935 Throwing cards away I Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the bottom. The following operation is performed as long as there are at least two cards in the deck: Throw away the to

UVa 10382 - Watering Grass 解题心得

原题: n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each sprinkler is installed at the horizontal center line of the strip. For each sprinkler we are given its position as the distance from the left end of t

UVA 10020 - Minimal coverage 解题心得

原题: The Problem Given several segments of line (int the X axis) with coordinates [Li,Ri]. You are to choose the minimal amount of them, such they would completely cover the segment [0,M]. The Input The first line is the number of test cases, followed