Description

 

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 <stack>
using namespace std;
int main()
{
int n;
cin >> n;
cin.get();
while (n--)
{
stack<char>st_ch;
int loge = 1;
char c;
while (cin.get(c) && c != '\n')
{
if (c == ')')
{
if (st_ch.empty())
loge = 0;
else if (st_ch.top() == '(')
st_ch.pop();
else
loge = 0;
}
else if (c == ']')
{
if (st_ch.empty())
loge = 0;
else if (st_ch.top() == '[')
st_ch.pop();
else
loge = 0;
}
else
st_ch.push(c);
}
if (st_ch.empty()&&loge)
cout << "Yes" << endl;
else
cout << "No" << endl; }
return 0; }

Problem A 栈的更多相关文章

  1. hdu Train Problem I(栈的简单应用)

    Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...

  2. Train Problem I(栈)

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  3. train problem I (栈水题)

    杭电1002http://acm.hdu.edu.cn/showproblem.php?pid=1022 Train Problem I Time Limit: 2000/1000 MS (Java/ ...

  4. Hdu 1022 Train Problem I 栈

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. Train Problem(栈的应用)

    Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of studen ...

  6. HDU1022 Train Problem I 栈的模拟

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1042 栈的模拟,题目大意是已知元素次序, 判断出栈次序是否合理. 需要考虑到各种情况, 分类处理. 常 ...

  7. Problem K 栈

    Description A math instructor is too lazy to grade a question in the exam papers in which students a ...

  8. Problem D: 栈小游戏

    #include <iostream> #include <vector> #include <stack> #include <algorithm> ...

  9. CF 990C. Bracket Sequences Concatenation Problem【栈/括号匹配】

    [链接]:CF [题意]: 给出n个字符串,保证只包含'('和')',求从中取2个字符串链接后形成正确的括号序列的方案数(每个串都可以重复使用)(像'()()'和'(())'这样的都是合法的,像')( ...

随机推荐

  1. D3.js 简介和安装

    一.What´s D3.js D3.js是一种数据操作类型的javascript库(也可视其为插件):结合HTML,SVG和CSS,D3可以图形化的,生动的展现数据. D3 的全称是(Data-Dri ...

  2. 【matlab】读写文件

    save('pqfile.mat','M'); ('E:\我的坚果云\pqfile.mat','M'); 其他: http://blog.csdn.net/iqizheng/article/detai ...

  3. entity reference在views中的运用

    一个views block可以获取从url过来的nid,某个node的有entity reference字段, 填入一点数据,然后在views里关联一下这个entity reference field ...

  4. 小韦XP 和win2003装电信天翼3G无线上网卡

    拷贝文件.bat @echo 开始注册echo n|copy /-y msports.dll %windir%\system32\regsvr32 /s %windir%\system32\mspor ...

  5. java 集合(Set1)

    ----------------|Collection(为什么要画这个图?学多了之后该忘了) --------------------------|List --------------------- ...

  6. 本地化Model Factory

    即让其生成中文的测试数据 先简单介绍Model Factory两个常用方法: 进入tinker页面生成测试数据: Factory(User::class,10)->make():make()方法 ...

  7. poj-----(2828)Buy Tickets(线段树单点更新)

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 12930   Accepted: 6412 Desc ...

  8. Multiply Strings [LeetCode]

    Problem Description http://oj.leetcode.com/problems/multiply-strings/ Basic idea is to multiply two ...

  9. iOS 开发 – 均衡代码职责

    前言 文章的标题有点绕口,不过想了半天,想不到更好的标题了.本文的诞生有一部分功劳要归于iOS应用现状分析,标题也是来源于原文中的"能把代码职责均衡的划分到不同的功能类里".如果你 ...

  10. 转载解决:错误的语法:”XXXX“必须是批处理中仅有的语句

    SQL Server 数据库提示“错误的语法:”XXXX“必须是批处理中仅有的语句”报错的原因分析 解析:批处理必须以 CREATE 语句开始.也就是说一个查询分析器里面只有一个批处理语句才是规范的语 ...