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. 都学Python了,C++难道真的用不着了吗?

    本文首发 | 公众号:lunvey 人人都在学Python,我还学C++吗? 现在只要提及编程语言,得到的答复都是:学Python,有未来!   大家可能有一个误区,数据分析带火了Python,让人们 ...

  2. Go的包

    目录 go的包 一.包的创建规则 二.包的导入规则 三.包的函数调用 go的包 一.包的创建规则 一个包就是一个文件夹. 同一个包(文件夹)下,所有go文件都只能用同一个package,也就是每个文件 ...

  3. 第39天学习打卡(UDP多线程在线咨询 URL)

    UDP多线程在线咨询  package com.kuang.chat; ​ import java.io.BufferedReader; import java.io.InputStreamReade ...

  4. Python 过滤字母和数字

    [前言]在写爬虫时,正则表达式有时候比较难写,一个是自己不熟练,二者数据分析提取数据千奇百怪. 一.好在python有个re模块,提供了很多更加简便的方法:可参考此文档:https://www.cnb ...

  5. 异常控制流(csapp)

    [前言]程序按照一定顺序执行称为控制转移.最简单的是平滑流,跳转.调用和返回等指令会造成平滑流的突变.系统也需要能够对系统状态的变化做出反应,这些系统状态不能被内部程序变量捕获但是,操作系统通过使控制 ...

  6. WEB容器开启、关闭OPTIONS方法

    发现 请求包随意,响应包信息如下: HTTP/1.1 200 OK Cache-Control: private Content-Type: text/html; charset=utf-8 Vary ...

  7. 《C++ Primer》笔记 第11章 关联容器

    关联容器类型 解释 按关键字有序保存元素 -- map 关联数组:保存关键字-值对 set 关键字即值,即只保存关键字的容器 multimap 关键字可重复出现的map multiset 关键字可重复 ...

  8. iot漏洞入门

    路由器漏洞入门 下载项目https://github.com/praetorian-inc/DVRF 安装quem sudo apt install qemu-user-static 安装gdb-mu ...

  9. 2019 GDUT Rating Contest II : Problem B. Hoofball

    题面: 传送门 B. Hoofball Input file: standard input Output file: standard output Time limit: 5 second Memor ...

  10. 攻防世界 reverse debug

    debug  XCTF 3rd-GCTF-2017 .net程序,这里我用的dnspy,当然.net Reflector也很好用. 查看程序,发现是明文比较,下断,debug,完成. flag{967 ...