UVa 673 (括号配对) Parentheses Balance

本来是当做水题来做的,后来发现这道题略坑。

首先输入的字符串可能是空串,所以我用了gets函数,紧接着就被scanf("%d", &n)后面的换行符坑掉了。

于是乎再加一句getchar()

 1 #include <cstdio>
 2 #include <stack>
 3 #include <cstring>
 4 using namespace std;
 5
 6 const int maxn = 150;
 7 char s[maxn];
 8
 9 bool ok(const char& c1, const char& c2)
10 {
11     if(c1 == ‘(‘ && c2 == ‘)‘) return true;
12     if(c1 == ‘[‘ && c2 == ‘]‘) return true;
13     return false;
14 }
15
16 int main()
17 {
18     //freopen("in.txt", "r", stdin);
19
20     int n;
21     scanf("%d", &n); getchar();
22     while(n--)
23     {
24         gets(s);
25         stack<char> S;
26         while(!S.empty()) S.pop();
27         int l = strlen(s);
28         if(l % 2 != 0)
29         {
30             puts("No");
31             continue;
32         }
33         bool flag = true;
34         for(int i = 0; i < l; ++i)
35         {
36             if(s[i] == ‘(‘ || s[i] == ‘[‘) S.push(s[i]);
37             else
38             {
39                 if(S.empty()) { flag = false; break; }
40                 else if(ok(S.top(), s[i])) S.pop();
41                 else { flag = false; break; }
42             }
43         }
44         if(!S.empty()) flag = false;
45         printf("%s\n", flag ? "Yes" : "No");
46     }
47
48     return 0;
49 }

代码君

时间: 2024-08-02 13:18:02

UVa 673 (括号配对) Parentheses Balance的相关文章

UVa 673 括号平衡

思路:简单的匹配操作,利用栈. Code: #include<stdio.h> #include<string.h> char stack[135]; int main() { int n; scanf("%d",&n); getchar(); while(n-->0) { memset(stack,0,sizeof(stack)); char c; int top=0; int flag=1; while((c=getchar())!='\n')

UVA 673(括号匹配)

本地单文件上传脚本,命名uf 这是在本机上做的测试,利用bpcs_uploader脚本实现,只是进行简单的封装,自动完善云端文件路径. 技术要点:使用dirname获取文件所在目录,使用pwd获取文件完整路径,并作为云端文件路径. #!/bin/bash cur_dir=$(cd "$(dirname "$1")"; pwd) name=$(basename "$1") /home/grm/bin/bpcs_uploader/bpcs_uploa

uva 673括号匹配问题

注意:空行要输出Yes,要用gets读函数,这道题让我学会了gets和scanf函数除了空格问题之外还是 有区别的,scanf把空格,Tab, 回车都视为结束标志,不会读入他们,后面自动加'\0',所以空格,回车,Tab 都在缓冲区,可以用getchar()读取到,但是gets()函数虽然也将回车视为结束标志,但是他们会将回车读 入,读到字符串里是'\0',所以,他们就不会存在缓冲区里,也就是说用getchar()读取不到他们了... 放代码: #include<stdio.h> #inclu

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 (栈的应用!)

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 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 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

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)