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.

Output

A sequence of Yes or No on the output file.

Sample Input

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

Sample Output

Yes
No
Yes 用栈模拟匹配括号

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
int compare(char p,char q)
{
if(p=='('&&q==')') return 1;
if(p=='['&&q==']') return 1;
else return 0;
}
int main()
{
int n,L,flag;
char c[100];

scanf("%d\n",&n);
for(int i=0; i<n; i++)
{
stack<char> st;
flag=0;
gets(c);
L=strlen(c);
for(int j=0; j<L; j++)
{

if(c[j]=='('||c[j]=='[') st.push(c[j]);
if(c[j]==')'||c[j]==']')
{
if(st.empty())
{
flag=1;
break;
}
if(compare(st.top(),c[j])) st.pop();
else{
flag=1;
break;
}
}
}
if(flag==1||!st.empty()) cout<<"No"<<endl;
else cout<<"Yes"<<endl;

}
return 0;
}

chapter6 数据结构基础之习题 Parentheses Balance的更多相关文章

  1. 算法与数据结构基础 - 分治法(Divide and Conquer)

    分治法基础 分治法(Divide and Conquer)顾名思义,思想核心是将问题拆分为子问题,对子问题求解.最终合并结果,分治法用伪代码表示如下: function f(input x size ...

  2. 算法与数据结构基础 - 贪心(Greedy)

    贪心基础 贪心(Greedy)常用于解决最优问题,以期通过某种策略获得一系列局部最优解.从而求得整体最优解. 贪心从局部最优角度考虑,只适用于具备无后效性的问题,即某个状态以前的过程不影响以后的状态. ...

  3. 算法与数据结构基础 - 深度优先搜索(DFS)

    DFS基础 深度优先搜索(Depth First Search)是一种搜索思路,相比广度优先搜索(BFS),DFS对每一个分枝路径深入到不能再深入为止,其应用于树/图的遍历.嵌套关系处理.回溯等,可以 ...

  4. UVa 673 Parentheses Balance -SilverN

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

  5. UVa 673 Parentheses Balance

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

  6. UVa673 Parentheses Balance

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

  7. Python之数据结构基础

    一.数据结构基础     a.什么是数据结构          b.数据结构的分类         c.列表           import random from timewrap import ...

  8. 数据结构基础(1)--数组C语言实现--动态内存分配

    数据结构基础(1)--数组C语言实现--动态内存分配 基本思想:数组是最常用的数据结构,在内存中连续存储,可以静态初始化(int a[2]={1,2}),可以动态初始化 malloc(). 难点就是数 ...

  9. php数据结构课程---1、数据结构基础介绍(程序是什么)

    php数据结构课程---1.数据结构基础介绍(程序是什么) 一.总结 一句话总结: 程序=数据结构+算法 设计好数据结构,程序就等于成功了一半. 数据结构是程序设计的基石. 1.数据的逻辑结构和物理结 ...

随机推荐

  1. sysbench_fileio.sh

    当我--file-total-size=30G 并且指定3个文件的时候,结果是产生3个10G的文件,然而--max-time=20被忽视了,虽然指定了20s的限制,实际上是在prepare阶段,--m ...

  2. poj2385 - Apple Catching【动态规划】

    Description It is a little known fact that cows love apples. Farmer John has two apple trees (which ...

  3. JAVA学习总结-常用数据结构

    java中集合框架其实就是数据结构的实现的封装; 参考资料:任小龙教学视频 1,什么是数据结构? 数据结构是计算机存储,组织数据的方式; 数据结构是指相互之间存在一种或多种特定关系的数据元素的集合; ...

  4. Java 初学者

    在有C++和C#基础之下开始学习Java,主要记录了一些和C++C#不同的或不知到的点 栈对象必须初始化,否则会报错.(其他的则有默认值) byte占用8位,char占用16位 接口默认为public ...

  5. PAT 1108 Finding Average

    The basic task is simple: given N real numbers, you are supposed to calculate their average. But wha ...

  6. 曾经遇过的sql问题

    曾经遇过的sql问题 问题一: 语句1: select SUM(level) from Comment 语句2: ELSE SUM(level) END as totalLevel from Comm ...

  7. 【Codeforces 158B】Taxi

    [链接] 我是链接,点我呀:) [题意] 每辆车可以载重4个人. 一共有n个组,每个组分别有s[i]个人. 要求每个组的人都在同一辆车里面. 问最少需要多少辆车 [题解] 将每个组的人数从小到大排序. ...

  8. play snake on windows

    今天和人吃晚饭突然想起来 之前西佳佳老师说小学期会要求两星期撸一个小游戏 有人已经撸完一个俄罗斯方块了... 菜逼我决定从最简单的贪吃蛇玩起... 我是直接参考的这个博客 算是相当简单而且很Low的实 ...

  9. NEMA-0183(GPRMC GPGGA)详细解释

    NEMA-0183(GPRMC GPGGA)详细解释 nmea数据如下:  $GPGGA,121252.000,3937.3032,N,11611.6046,E,1,05,2.0,45.9,M,-5. ...

  10. hive计算日期差

    首先,hive本身有一个UDF,名字是datediff.我们来看一下这个日期差计算的官方描述,(下面这个是怎么出来的): hive> desc function extended datedif ...