栈及其DFS:B - Parentheses Balance
解题心得及总结:
总结:
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
#include<stdio.h>
#include<stack>
//是c++中的函数,不可以加.h
#include<iostream>
using namespace std;
int main()
{
char aim[140],now,emp;
int i,j,n,c,length;
bool flag = false;
scanf("%d",&n);;
getchar();
while(n--)
{
i = 0;
flag = false;
//判断是否为空格和是否有右括号压入栈中
stack <char> st;
//栈的定义
memset(aim,0,sizeof(aim));
gets(aim);
//注意gets的坑
length = strlen(aim);
for(i=0;i<length;i++)
{
if(st.empty())
//当栈为空的时候只能够压入,不能取出。
{
st.push(aim[i]);
if(st.top() == ' ')
{
printf("Yes\n");
flag = true;
break;
}
}
else
{
if(!st.empty())
{
now = st.top();
if(now == ')' || now == ']')
{
printf("No\n");
flag = true;
break;
}
}
if(st.empty())
continue;
if(now == '(')
{
if(aim[i] == ')')
{
st.pop();
}
else
st.push(aim[i]);
}
if(now == '[')
{
if(aim[i] == ']')
{
st.pop();
}
else
st.push(aim[i]);
}
}
}
if(flag)
continue;
if(st.empty())
printf("Yes\n");
else
printf("No\n");
}
}
栈及其DFS:B - Parentheses Balance的更多相关文章
- UVa 673 Parentheses Balance -SilverN
You are given a string consisting of parentheses () and []. A string of this type is said to be corr ...
- UVa673 Parentheses Balance
// UVa673 Parentheses Balance // 题意:输入一个包含()和[]的括号序列,判断是否合法. // 具体递归定义如下:1.空串合法:2.如果A和B都合法,则AB合法:3.如 ...
- UVa 673 Parentheses Balance
一个匹配左右括号的问题 /*UVa 673 Parentheses Balance*/ #include<iostream> #include<algorithm> #incl ...
- UVALive 3486/zoj 2615 Cells(栈模拟dfs)
这道题在LA是挂掉了,不过还好,zoj上也有这道题. 题意:好大一颗树,询问父子关系..考虑最坏的情况,30w层,2000w个点,询问100w次,貌似连dfs一遍都会TLE. 安心啦,这肯定是一道正常 ...
- Code POJ - 1780(栈模拟dfs)
题意: 就是数位哈密顿回路 解析: 是就算了...尼玛还不能直接用dfs,得手动开栈模拟dfs emm...看了老大半天才看的一知半解 #include <iostream> #inclu ...
- 【栈模拟dfs】Cells UVALive - 3486
题目链接:https://cn.vjudge.net/contest/209473#problem/D 题目大意:有一棵树,这棵树的前n个节点拥有子节点,告诉你n的大小,以及这n个节点各有的子节点个数 ...
- 队列和 BFS —— 栈和 DFS
队列和 BFS: 广度优先搜索(BFS)的一个常见应用是找出从根结点到目标结点的最短路径. 示例 这里我们提供一个示例来说明如何使用 BFS 来找出根结点 A 和目标结点 G 之间的最短路径. 洞悉 ...
- 百炼3752:走迷宫--栈实现dfs
3752:走迷宫 总时间限制: 1000ms 内存限制: 65536kB 描述 一个迷宫由R行C列格子组成,有的格子里有障碍物,不能走:有的格子是空地,可以走.给定一个迷宫,求从左上角走到右下角最 ...
- UVa 673 Parentheses Balance(栈的使用)
栈 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description You are ...
随机推荐
- python socket客户端
#./usr/bin/env python#coding:utf-8import socket#.....ip...print ('server start...')ip_port = ('144.3 ...
- zabbix文档3.4-7配置
zabbix文档3.4-7配置 1 主机和主机组 典型的Zabbix主机是您希望监视的设备(服务器,工作站,交换机等). 创建主机是Zabbix中首个监控任务之一.例如,如果要监视服务器"x ...
- TP5.0:同一个控制器访问不同方法
首先,我把TP框架的内容放置在manualtp5文件夹 在manualtp5/application/index/controller/index控制器中定义两个方法: 我们都知道,如果我们网址中不输 ...
- UOJ#130 【NOI2015】荷马史诗 K叉哈夫曼树
[NOI2015]荷马史诗 链接:http://uoj.ac/problem/130 因为不能有前缀关系,所以单词均为叶子节点,就是K叉哈夫曼树.第一问直接求解,第二问即第二关键字为树的高度. #in ...
- [转载]AngularJS入门教程02:AngularJS模板
是时候给这些网页来点动态特性了——用AngularJS!我们这里为后面要加入的控制器添加了一个测试. 一个应用的代码架构有很多种.对于AngularJS应用,我们鼓励使用模型-视图-控制器(MVC)模 ...
- POJ 1631 Bridging signals(LIS的等价表述)
把左边固定,看右边,要求线不相交,编号满足单调性,其实是LIS的等价表述. (如果编号是乱的也可以把它有序化就像Uva 10635 Prince and Princess那样 O(nlogn) #in ...
- 黑幕背后的Autorelease
http://blog.sunnyxx.com/2014/10/15/behind-autorelease/ 我是前言 Autorelease机制是iOS开发者管理对象内存的好伙伴,MRC中,调用[o ...
- Gym - 100676H Capital City(边强连通分量 + 树的直径)
H. Capital City[ Color: Black ]Bahosain has become the president of Byteland, he is doing his best t ...
- PAT (Basic Level) Practise (中文)- 1003. 我要通过!(20)
http://www.patest.cn/contests/pat-b-practise/1003 “答案正确”是自动判题系统给出的最令人欢喜的回复.本题属于PAT的“答案正确”大派送 —— 只要读入 ...
- C++的XML编程经验――LIBXML2库使用指南
C++的XML编程经验――LIBXML2库使用指南 写这篇文章的原因有如下几点:1)C++标准库中没有操作XML的方法,用C++操作XML文件必须熟悉一种函数库,LIBXML2是其中一种很优秀的XML ...