解题心得及总结:

总结:

1、递推:又1推出n,数列中的基本到通项,最终目标得出通项公式。

递归:又n先压缩栈到1,再从函数的出口找到1,又1到n,再从n计算到1;

2、判断是否可以由递推或递推得出,再判断可以用BFS or DFS得出,BFS使用队列(queue),DFS使用栈(stack)。

3、队列,先进先出。如图:


栈先进后出,又称先进后出表。

例题心得:

1、关键点:队列是否为空,括号是否单项匹配。注意:单项匹配,只能为队列中的左括号和数组中的右括号相互消去。而数列中不能压入右括号,不然直接跳出(可以看作是剪枝)。

2、数列开大一点,汗~~~!

3、这题有坑,输入空格时会输出“Yes”(审题第一个要求)。

4、由于有输入的坑,所以在输入时会纠结,cin,scanf,不能输出空格,只能使用gets(gets可以输入空格和上一个输入的回车),但是在使用gets时会将上一个回车输入导致输入混乱,所以可以在gets前面加一个getchar()将无用的回车去掉。

5、全局变量中的int、char、long long以及结构体中的元素会自动初始化为0。

例题:

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

  1. #include<stdio.h>
  2. #include<stack>
  3.  //是c++中的函数,不可以加.h
  4. #include<iostream>
  5. using namespace std;
  6. int main()
  7. {
  8. char aim[140],now,emp;
  9. int i,j,n,c,length;
  10. bool flag = false;
  11. scanf("%d",&n);;
  12. getchar();
  13. while(n--)
  14. {
  15. i = 0;
  16. flag = false;
  17. //判断是否为空格和是否有右括号压入栈中
  18. stack <char> st;
  19. //栈的定义
  20. memset(aim,0,sizeof(aim));
  21. gets(aim);
  22. //注意gets的坑
  23. length = strlen(aim);
  24. for(i=0;i<length;i++)
  25. {
  26. if(st.empty())
  27. //当栈为空的时候只能够压入,不能取出。
  28. {
  29. st.push(aim[i]);
  30. if(st.top() == ' ')
  31. {
  32. printf("Yes\n");
  33. flag = true;
  34. break;
  35. }
  36. }
  37. else
  38. {
  39. if(!st.empty())
  40. {
  41. now = st.top();
  42. if(now == ')' || now == ']')
  43. {
  44. printf("No\n");
  45. flag = true;
  46. break;
  47. }
  48. }
  49. if(st.empty())
  50. continue;
  51. if(now == '(')
  52. {
  53. if(aim[i] == ')')
  54. {
  55. st.pop();
  56. }
  57. else
  58. st.push(aim[i]);
  59. }
  60. if(now == '[')
  61. {
  62. if(aim[i] == ']')
  63. {
  64. st.pop();
  65. }
  66. else
  67. st.push(aim[i]);
  68. }
  69. }
  70. }
  71. if(flag)
  72. continue;
  73. if(st.empty())
  74. printf("Yes\n");
  75. else
  76. printf("No\n");
  77. }
  78. }

栈及其DFS:B - Parentheses Balance的更多相关文章

  1. UVa 673 Parentheses Balance -SilverN

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

  2. UVa673 Parentheses Balance

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

  3. UVa 673 Parentheses Balance

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

  4. UVALive 3486/zoj 2615 Cells(栈模拟dfs)

    这道题在LA是挂掉了,不过还好,zoj上也有这道题. 题意:好大一颗树,询问父子关系..考虑最坏的情况,30w层,2000w个点,询问100w次,貌似连dfs一遍都会TLE. 安心啦,这肯定是一道正常 ...

  5. Code POJ - 1780(栈模拟dfs)

    题意: 就是数位哈密顿回路 解析: 是就算了...尼玛还不能直接用dfs,得手动开栈模拟dfs emm...看了老大半天才看的一知半解 #include <iostream> #inclu ...

  6. 【栈模拟dfs】Cells UVALive - 3486

    题目链接:https://cn.vjudge.net/contest/209473#problem/D 题目大意:有一棵树,这棵树的前n个节点拥有子节点,告诉你n的大小,以及这n个节点各有的子节点个数 ...

  7. 队列和 BFS —— 栈和 DFS

    队列和 BFS: 广度优先搜索(BFS)的一个常见应用是找出从根结点到目标结点的最短路径. 示例 这里我们提供一个示例来说明如何使用 BFS 来找出根结点 A 和目标结点 G 之间的最短路径. 洞悉 ...

  8. 百炼3752:走迷宫--栈实现dfs

    3752:走迷宫 总时间限制:  1000ms 内存限制:  65536kB 描述 一个迷宫由R行C列格子组成,有的格子里有障碍物,不能走:有的格子是空地,可以走.给定一个迷宫,求从左上角走到右下角最 ...

  9. UVa 673 Parentheses Balance(栈的使用)

     栈 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Description You are ...

随机推荐

  1. Python3基础(5)常用模块:time、datetime、random、os、sys、shutil、shelve、xml处理、ConfigParser、hashlib、re

    ---------------个人学习笔记--------------- ----------------本文作者吴疆-------------- ------点击此处链接至博客园原文------ 1 ...

  2. jquery.validate+jquery.form表单验证提交

    1.通过jquery.validate的submitHandler选项,即当表单通过验证时运行回调函数.在这个回调函数中通过jquery.form来提交表单: <script type=&quo ...

  3. 微信退款和支付宝退款接口调用(java版)

    项目中需要使用到微信和支付宝的退款功能,在这两天研究了一下这两个平台的退款,有很多坑,在开发中需要留意 1.微信退款接口 相对来说我感觉微信的退款接口还是比较好调用的,直接发送httppost请求即可 ...

  4. 一个关于laravel部署的讲座

    https://pusher.com/sessions/meetup/laravel-nigeria/deploying-your-laravel-application

  5. jQuery动态添加元素,并提交json格式数据到后台

    参考:https://www.cnblogs.com/shj-com/p/7878727.html 下载 下载该插件的地址是:http://www.bootcdn.cn/jquery.serializ ...

  6. NC57访问报错:java.sql.SQLException: Io 异常: Got minus one from a read call

    一.报错信息 1.  前端登录界面 2.  后台应用日志 报错信息一致为: $$callid= $$thread=[Service Monitor and Runtime Enroment] $$ho ...

  7. Oracle Form个性化案例(一)

    业务场景: 现有Form A,需通过A中的菜单栏中调用另一Form B,需将某值作为参数传入Form B中:

  8. .NET中异常类(Exception)

    异常:程序在运行期间发生的错误.异常对象就是封装这些错误的对象. try{}catch{}是非常重要的,捕获try程序块中所有发生的异常,如果没有捕获异常的话,程序运行的线程将会挂掉,更严重的是这些错 ...

  9. 工作流性能优化(敢问activiti有扩展性?)(1)

    工作流待办(首页待办列表),加载缓慢,activiti本机,看了代码又是全部数据加载到内存,然后代码过滤,我为什么又说又呢? 用VisualVM做性能测试:   之前同事给的解决方案: 1.把&quo ...

  10. 【洛谷1501】[国家集训队] Tree II(LCT维护懒惰标记)

    点此看题面 大致题意: 有一棵初始边权全为\(1\)的树,四种操作:将两点间路径边权都加上一个数,删一条边.加一条新边,将两点间路径边权都加上一个数,询问两点间路径权值和. 序列版 这道题有一个序列版 ...