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

  1. 3
  2. ([])
  3. (([()])))
  4. ([()[]()])()

Sample Output

  1. Yes
  2. No
  3. Yes
    分析:
    这个括号配对问题很符合后进先出-栈的方法,遇到左括号时进栈,遇到右括号时将栈顶元素与之配对的括号出栈。
  1. #include <iostream>
  2. #include <stack>
  3. using namespace std;
  4. int main()
  5. {
  6. int n;
  7. cin >> n;
  8. cin.get();
  9. while (n--)
  10. {
  11. stack<char>st_ch;
  12. int loge = 1;
  13. char c;
  14. while (cin.get(c) && c != '\n')
  15. {
  16. if (c == ')')
  17. {
  18. if (st_ch.empty())
  19. loge = 0;
  20. else if (st_ch.top() == '(')
  21. st_ch.pop();
  22. else
  23. loge = 0;
  24. }
  25. else if (c == ']')
  26. {
  27. if (st_ch.empty())
  28. loge = 0;
  29. else if (st_ch.top() == '[')
  30. st_ch.pop();
  31. else
  32. loge = 0;
  33. }
  34. else
  35. st_ch.push(c);
  36. }
  37. if (st_ch.empty()&&loge)
  38. cout << "Yes" << endl;
  39. else
  40. cout << "No" << endl;
  41.  
  42. }
  43. return 0;
  44.  
  45. }

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. Spring Boot 以 jar 包方式运行在后台

    spring-boot jar 包方式启动: 首先,为了防止和常用的 Tomcat 8080 端口冲突,将 Spring-boot 项目的端口号设置为 9090. 具体方法:在 application ...

  2. 论APP测试中黑盒测试方案的重要性?

    运筹帷幄之中,决胜千里之外.古人足不出户,通过正确的部署就能决定千里之外战争的胜利!而于测试人员而言,制定正确的测试方案,就是日后测试就是是否顺利的决定性因素. 在整个测试过程中,对测试人员.资源以及 ...

  3. 【转】APP的缓存文件到底应该存在哪?看完这篇文章你应该就自己清楚了

    只要是需要进行联网获取数据的APP,那么不管是版本更新,还是图片缓存,都会在本地产生缓存文件.那么,这些缓存文件到底放在什地方合适呢?系统有没有给我们提供建议的缓存位置呢?不同的缓存位置有什么不同呢? ...

  4. 打不开chm文件解决办法

    打不开chm文件解决办法.bat regsvr32 itss.dll /sregsvr32 hhctrl.ocx /s

  5. Docker 使用指南 (一)—— 基本操作

    版权声明:本文由田飞雨原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/98来源:腾云阁 https://www.qcloud ...

  6. Java客户端通过Http发送POST请求上传文件到web服务器

    http://www.cnblogs.com/WilliamJiang/archive/2012/04/29/2475883.html 1.朋友的一个需求,让我给他实现,需求是这样的,需要用ASP.n ...

  7. Compound Interest Calculator3.0续

    1.你写的程序能让客户随意操作吗?误输入数据.不小心做了非常规的操作程序是什么反应? 2.如果向银行贷款10万元,年利率6.5%,期限为10年,那么每月等额本息还款多少?(算复利条件下等额还款金额) ...

  8. BeautifulSoup 常用方法

    #输出所得标签的‘’属性值 获取 head里面的第一个meta的content值 soup.head.meta['content'] 获取第一个span的内容 soup.span.string 获取第 ...

  9. centos修改文件及文件夹权限

    查看文件权限的语句: 在终端输入:ls -l xxx.xxx (xxx.xxx是文件名) 那么就会出现相类似的信息,主要都是这些:-rw-rw-r-- 一共有10位数 其中: 最前面那个 - 代表的是 ...

  10. android之merge布局

    <merge />标签闪亮登场了.当LayoutInflater遇到这个标签时,它会跳过它,并将<merge />内的元素添加到<merge />的父元素里. 用& ...