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. JavaScriptBOM操作

    BOM(浏览器对象模型)主要用于管理浏览器窗口,它提供了大量独立的.可以与浏览器窗口进行互动的功能,这些功能与任何网页内容无关.浏览器窗口的window对象是BOM的顶层对象,其他对象都是该对象的子对 ...

  2. 线段树&数链剖分

    傻逼线段树,傻逼数剖 线段树 定义: 线段树是一种二叉搜索树,与区间树相似,它将一个区间划分成一些单元区间,每个单元区间对应线段树中的一个叶结点. 使用线段树可以快速的查找某一个节点在若干条线段中出现 ...

  3. 用OkHttpGo和FastJson获取OneNET云平台数据(解析嵌套数组)

    JSON数据格式有两种,一种是 { } 大括号表示的JSON对象,一种是 [ ] 中括号表示的JSON数组.从OneNET获取到的数组是这样的,并用Json解析网址查看https://jsonform ...

  4. 【不在混淆的C】指针函数、函数指针、回调函数

    一.指针函数 函数的返回值是指针类型. int* fun(int a,int b); 指针函数使用: 返回字符串 这里要注意,"1234567890abc"是字符串常量,*p指向的 ...

  5. 【JAVA并发第四篇】线程安全

    1.线程安全 多个线程对同一个共享变量进行读写操作时可能产生不可预见的结果,这就是线程安全问题. 线程安全的核心点就是共享变量,只有在共享变量的情况下才会有线程安全问题.这里说的共享变量,是指多个线程 ...

  6. 第39天学习打卡(多线程 Thread Runnable 初始并发问题 Callable )

    多线程详解 01线程简介 Process与Thread 程序:是指令和数据的有序集合,其本身没有任何运行的含义,是一个静态的概念. 进程则是执行程序的一次执行过程,它是一个动态的概念.是系统资源分配的 ...

  7. LeetCode113. 路径总和 II

    原题链接 1 class Solution: 2 def pathSum(self, root: TreeNode, sum: int) -> List[List[int]]: 3 ans,tm ...

  8. Linux文本三剑客总结

    Linux文本处理三剑客 grep 文本过滤(模式:pattern)工具 grep, egrep, fgrep(不支持正则表达式搜索) grep  grep: Global search REgula ...

  9. SpringDataJPA 入门

    前言 1. 三者的区别与联系 JPA:本身是一种ORM规范,不是ORM框架.由各大ORM框架提供实现. Hibernate是一个完整的ORM框架,常规CRUD我们不需要写一句SQL;框架比较重,学习成 ...

  10. Windows搭建flutter开发环境以及android&idea配置

    Flutter:是谷歌新推出的一款能够支持Android和IOS跨平台开发的全新的UI框架. 拥有自己的一套UI渲染引擎,所以目前的测试数据来看,在性能上面,并没有比原生App性能低多少,所以目前来看 ...