一个匹配左右括号的问题

/*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的更多相关文章

  1. UVa 673 Parentheses Balance -SilverN

    You are given a string consisting of parentheses () and []. A string of this type is said to be corr ...

  2. UVa 673 Parentheses Balance(栈的使用)

     栈 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Description You are ...

  3. UVa 673 Parentheses Balance【栈】

    题意:输入一个包含"()"和"[]"的序列,判断是否合法 用栈来模拟,遇到"(",“[”就入栈,遇到')',']'就取出栈顶元素看是否匹配, ...

  4. UVA 673 Parentheses Balance (栈)

    题意描述: 给出一段只包含()和[]的字符串,判断是否合法,合法输出YES,不合法输出NO 规则: 1.该串为空,则合法 2.若A合法,B合法,则AB合法 3.若A合法,则(A)和[A]均合法 解题思 ...

  5. UVa 673 Parentheses Balance (stack)

    题目描述 : 判断字符串是不是符合正确的表达式形式. 要点 : 考虑字符串为空的时候,用getline输入,每一次判断后如果为No则要清空栈.对称思想. 注意输入格式. 代码: #include &l ...

  6. 【UVA】673 Parentheses Balance(栈处理表达式)

    题目 题目     分析 写了个平淡无奇的栈处理表达式,在WA了5发后发现,我没处理空串,,,,(或者说鲁棒性差?     代码 #include <bits/stdc++.h> usin ...

  7. UVa673 Parentheses Balance

    // UVa673 Parentheses Balance // 题意:输入一个包含()和[]的括号序列,判断是否合法. // 具体递归定义如下:1.空串合法:2.如果A和B都合法,则AB合法:3.如 ...

  8. UVA 673 (13.08.17)

     Parentheses Balance  You are given a string consisting of parentheses () and []. Astring of this ty ...

  9. Parentheses Balance UVA - 673

    You are given a string consisting of parentheses () and []. A string of this type is said to be corr ...

随机推荐

  1. SQL 性能调优日常积累

    我们要做到不但会写SQL,还要做到写出性能优良的SQL,以下为笔者学习.摘录.并汇总部分资料与大家分享! (1)选择最有效率的表名顺序(只在基于规则的优化器中有效) ORACLE 的解析器按照从右到左 ...

  2. opencv6.2-imgproc图像处理模块之图像尺寸上的操作及阈值

    接opencv6.1-imgproc图像处理模块之平滑和形态学操作,顺带说一句在opencv中的in-place操作就是比如函数的输入图像和输出图像两个指针是相同的,那么就是in-place操作了.比 ...

  3. RabbitMQ集群环境搭建-4

    确保成功安装好JDK,erlang,RabbitMQ等,并且RabbitMQ能正常启动,多台电脑之间能互相ping得通. 1. 安装 erlang.rabbitmq 如: 192.168.1.1.19 ...

  4. 出现“System.Data.SqlClient.SqlError: 尚未备份数据库的日志尾部”错误的解决方案

    Sql Server2008数据库在还原时出现如下错误信息:System.Data.SqlClient.SqlError: 尚未备份数据库<数据库名称>的日志尾部.如果该日志包含您不希望丢 ...

  5. word2vec 实践

    关于word2vec,这方面无论中英文的参考资料相当的多,英文方面既可以看官方推荐的论文,也可以看gensim作者Radim Řehůřek博士写得一些文章.而中文方面,推荐 @licstar的< ...

  6. java泛型中的对象

    import java.util.HashMap; class Key { String s; Key(String s) { this.s = new String(s); } @Override ...

  7. Memcached, Redis, MongoDB区别

    mongodb和memcached不是一个范畴内的东西.mongodb是文档型的非关系型数据库,其优势在于查询功能比较强大,能存储海量数据.mongodb和memcached不存在谁替换谁的问题. 和 ...

  8. Bete冲刺第一阶段

    Bete冲刺第一阶段 今日工作: github团队协作流程 web:调整dao层设计,增加新的dao组件 客户端:之前遗留的界面跳转的BUG 目前所遇问题: 第一,COCOAPODS的安装上还是有点问 ...

  9. js原型继承的几种方式

    1. 原型链继承 2,构造函数继承(对象冒充继承) 3,组合继承(原型链继承+构造函数继承) 4,原型式继承 5. 寄生组合式继承 一.原型链继承 function Show(){ this.name ...

  10. 天气预报API获取

    1.citycode: http://mobile.weather.com.cn/js/citylist.xml http://files.cnblogs.com/files/ys-wuhan/cit ...