UVa673 Parentheses Balance (栈)

链接:http://acm.hust.edu.cn/vjudge/problem/19102分析:特别要注意空串情况,还有括号奇数个肯定不合法都可以特判。
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <stack>
 5 using namespace std;
 6
 7 int main() {
 8     int n;
 9     cin >> n; getchar();
10     while (n--) {
11         string line;
12         getline(cin, line);
13         if (line.compare("\n") == 0) {
14             printf("Yes"); continue;
15         }
16         if (line.size() % 2) {
17             printf("No\n"); continue;
18         }
19         stack<char> s;
20         bool ok = true;
21         for (int i = 0; i < line.size(); i++) {
22             char ch = line[i];
23             if (ch == ‘(‘) s.push(ch);
24             else if (ch == ‘[‘) s.push(ch);
25             if (ch == ‘)‘)
26                 if (!s.empty() && s.top() == ‘(‘) s.pop();
27                 else { ok = false; break; }
28             else if (ch == ‘]‘)
29                 if (!s.empty() && s.top() == ‘[‘) s.pop();
30                 else { ok = false; break; }
31         }
32         if (ok && s.empty()) printf("Yes\n");
33         else printf("No\n");
34     }
35     return 0;
36 }
时间: 2024-11-10 08:25:06

UVa673 Parentheses Balance (栈)的相关文章

UVA-673 Parentheses Balance(栈)

题目大意:给一个括号串,看是否匹配. 题目分析:一开始用区间DP写的,超时了... 注意:空串合法. 代码如下: # include<iostream> # include<cstdio> # include<stack> # include<cstring> # include<algorithm> using namespace std; char p[130]; stack<char>s; bool judge() { int

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(括号配对 栈)

题意  判断输入的括号序列是否是配对的 栈的基础应用  栈顶元素与输入的字符匹配就出栈咯  注意括号序列可以为空 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 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 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

Week 1 # E Parentheses Balance

原题描述: E - 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)

UVA 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. W

leetcode题解:Valid Parentheses(栈的应用-括号匹配)

题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]&

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