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 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. Note that the string can contain arbitrary spaces and it can even be empty.

Output

A sequence of Yes or No on the output file.

Sample Input

3
([])
(([()])))
([()[]()])()

Sample Output

Yes
No
Yes

HINT

栈操作

Accepted

#include<bits/stdc++.h>
using namespace std; int main() {
ofstream fcout;
fcout.open("temp.txt");
int sum, flag = 0;
string s;
cin >> sum;getchar();
while (sum--) {
stack<char>S;
flag = 0;
getline(cin, s);
for (int i = 0;i < s.length();i++) {
if (s[i] == '['|| s[i] == '(')S.push(s[i]);
else if (s[i] == ')')
if (!S.empty() && S.top() == '(')S.pop();
else { flag = 1;break; }
else if (s[i] == ']')
if (!S.empty() && S.top() == '[')S.pop();
else { flag = 1;break; }
}
cout << (S.empty() && !flag ? "Yes" : "No") << endl;
}
}

Parentheses Balance UVA - 673的更多相关文章

  1. 平衡的括号 (Parentheses Balance UVA - 673)

    题目描述: 原题:https://vjudge.net/problem/UVA-673 题目思路: 1.水题 2.栈+模拟 3.坑在有空串 AC代码 #include <iostream> ...

  2. UVa 673 Parentheses Balance -SilverN

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

  3. UVa 673 Parentheses Balance

    一个匹配左右括号的问题 /*UVa 673 Parentheses Balance*/ #include<iostream> #include<algorithm> #incl ...

  4. UVa673 Parentheses Balance

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

  5. UVA 673 (13.08.17)

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

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

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

  7. UVa 673 Parentheses Balance【栈】

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

  8. UVa 673 (括号配对) Parentheses Balance

    本来是当做水题来做的,后来发现这道题略坑. 首先输入的字符串可能是空串,所以我用了gets函数,紧接着就被scanf("%d", &n)后面的换行符坑掉了. 于是乎再加一句 ...

  9. UVA 673 Parentheses Balance (栈)

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

随机推荐

  1. 一文读懂网管协议 - SNMP,NETCONF,RESTCONF

    本文篇幅较长,主要涉及以下内容: 介绍传统 CLI 配置网络设备存在的挑战,网管协议出现的背景 SNMP 原理,交互过程,以及 trade-off NETCONF 架构,交互过程 RESTCONF 架 ...

  2. (十一) 数据库查询处理之连接(Join)

    (十一) 数据库查询处理之连接(Join) 1. 连接操作的一个例子 把外层关系和内层关系中满足一定关系的属性值拼接成一个新的元组 一种现在仍然十分有用的优化思路Late Materializatio ...

  3. oracle 查看 FK constraint referenced_table及columns

    select uc.table_name, uc.r_constraint_name, ucc.table_name, listagg(ucc.column_name, ',') within gro ...

  4. Java基础语法:注释

    书写注释是一个非常好的习惯. 注释并不会被执行,是给我们写代码的人看的. Java中的注释有三种: 单行注释(Line comment) 多行注释(Block comment) 文档注释(JavaDo ...

  5. 后端程序员之路 18、朴素贝叶斯模型(Naive Bayesian Model,NBM)

    贝叶斯推断及其互联网应用(一):定理简介 - 阮一峰的网络日志http://www.ruanyifeng.com/blog/2011/08/bayesian_inference_part_one.ht ...

  6. jQuery实现全网热播视频

    <section id="play"> <h1>全网热播视频</h1> <ul> <li><img src=&qu ...

  7. CentOS7 安装 MySQL Cluster 7.6.7

    引用自:http://lemonlone.com/posts/mysql-ndb-cluster-install/ 仅做备份和配置文件更改 1.先在VMware中安装 CentOS-7-x86_64- ...

  8. Protobuf在Cmake中的正确使用

    Protobuf是google开发的一个序列化和反序列化的协议库,我们可以自己设计传递数据的格式,通过.proto文件定义我们的要传递的数据格式.例如,在深度学习中常用的ONNX交换模型就是使用.pr ...

  9. HDU_6695 Welcome Party 【思维】

    一.题目 Welcome Party 二.分析 最开始的时候分析错了,认为只要找两个类型中的最小差值就可以了,忽略了是求两个类型中最大值的最小差值. 那么可以对第一个类型进行从大到小排序,枚举这个类型 ...

  10. Python的web开发

    一.Web开发 Tcp   udp       Cs即客户端.服务器端编程,客户端和服务器端之间需要使用socket,约定协议.版本(协议使用的是tcp或者udp).Tcp协议和udp协议,指定地址和 ...