UVa 673 Parentheses Balance
一个匹配左右括号的问题
/*UVa 673 Parentheses Balance*/
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<stack>
using namespace std;
char c[500];
int n;
int pd(char q,char e){
if(q=='(' && e==')')return 1;
if(q=='[' && e==']')return 1;
return 0;
}
int main(){
scanf("%d\n",&n);
while(n--){
gets(c);
if(strcmp(c,"\n")==0){
printf("Yes");
continue;
}
int flag=0;
stack<char>st;
int L=strlen(c);
for(int i=0;i<L;i++){
if(c[i]=='(' || c[i]=='[') st.push(c[i]);
else{
if(st.empty()){
flag=1;
break;
}
if(pd(st.top(),c[i])==1) st.pop();
else{
flag=1;
break;
}
}
}
if(flag==1 || !st.empty())printf("No\n");
else printf("Yes\n");
}
return 0;
}
UVa 673 Parentheses Balance的更多相关文章
- UVa 673 Parentheses Balance -SilverN
You are given a string consisting of parentheses () and []. A string of this type is said to be corr ...
- UVa 673 Parentheses Balance(栈的使用)
栈 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description You are ...
- UVa 673 Parentheses Balance【栈】
题意:输入一个包含"()"和"[]"的序列,判断是否合法 用栈来模拟,遇到"(",“[”就入栈,遇到')',']'就取出栈顶元素看是否匹配, ...
- UVA 673 Parentheses Balance (栈)
题意描述: 给出一段只包含()和[]的字符串,判断是否合法,合法输出YES,不合法输出NO 规则: 1.该串为空,则合法 2.若A合法,B合法,则AB合法 3.若A合法,则(A)和[A]均合法 解题思 ...
- UVa 673 Parentheses Balance (stack)
题目描述 : 判断字符串是不是符合正确的表达式形式. 要点 : 考虑字符串为空的时候,用getline输入,每一次判断后如果为No则要清空栈.对称思想. 注意输入格式. 代码: #include &l ...
- 【UVA】673 Parentheses Balance(栈处理表达式)
题目 题目 分析 写了个平淡无奇的栈处理表达式,在WA了5发后发现,我没处理空串,,,,(或者说鲁棒性差? 代码 #include <bits/stdc++.h> usin ...
- UVa673 Parentheses Balance
// UVa673 Parentheses Balance // 题意:输入一个包含()和[]的括号序列,判断是否合法. // 具体递归定义如下:1.空串合法:2.如果A和B都合法,则AB合法:3.如 ...
- UVA 673 (13.08.17)
Parentheses Balance You are given a string consisting of parentheses () and []. Astring of this ty ...
- Parentheses Balance UVA - 673
You are given a string consisting of parentheses () and []. A string of this type is said to be corr ...
随机推荐
- performSelector的原理以及用法
一.performSelector调用和直接调用区别下面两段代码都在主线程中运行,我们在看别人代码时会发现有时会直接调用,有时会利用performSelector调用,今天看到有人在问这个问题,我便做 ...
- JS添加DOM元素CSS权重BUG
修改删除table的时候,比如拆分合并单元格,合并全部TR中的某个TD后在拆分还原,即使直接在td标签中设置了td的高宽属性,当td在css文件中设置为宽度auto的时候,不能显示出TD来,显示TD宽 ...
- node基础02:第一个node程序
1.第一个web服务器 var http = require("http"); http.createServer(function(request, response){ res ...
- Oracle:ODP.NET Managed 小试牛刀
“ODP.NET Managed”发布已经有一段时间了,近期正好有一个新项目,想尝试用一下,参考园子里的文章:<.NET Oracle Developer的福音——ODP.NET Managed ...
- 【转】加快网站访问速度——Yslow极限优化
Yslow是一套雅虎的网页评分系统,详细的列出了各项影响网页载入速度的参数,这里不做多说. 我之前就一直参考Yslow做博客优化,经过长时间的学习也算是有所收获,小博的YslowV2分数达到了94分( ...
- 扩展欧几里得算法(extgcd)
相信大家对欧几里得算法,即辗转相除法不陌生吧. 代码如下: int gcd(int a, int b){ return !b ? gcd(b, a % b) : a; } 而扩展欧几里得算法,顾名思义 ...
- 发布园友设计的新款博客皮肤BlueSky
园友#a为大家设计了一款“简单.纯粹,一点淡雅,一点宁静”的博客皮肤——BlueSky,欢迎您的享用!感谢#a的精心设计! 如果您有兴趣为大家设计博客皮肤,请将您设计的html/css/images文 ...
- 一道javascript面试题
下面表达式比较的结果分别是什么? 1. []=="0" 2. []==0 3. "0"==0 4. []==false 5. []==[] 大家可以试试写下自己 ...
- viso图插入Word中大片空白解决办法
按住CTRL-->将鼠标指向绘图页边缘-->指针变为移动符号(双向箭头)-->按下左键修改绘图页大小.
- getContentResolver()内容解析者查询联系人、插入联系人
首先,我们需要知道的两个Uri: 1.Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");//查到 ...