栈及其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 ...
随机推荐
- Aura Component Skills & Tools
本篇参考: https://trailhead.salesforce.com/content/learn/modules/lex_dev_lc_vf_fundamentals 不知不觉已经做了三年多的 ...
- java常见数据结构举例
1. ArrayList(参考) import java.util.*; public class Test{ public static void main(String [] args){ Arr ...
- matlab 打不开excel文件
方法论 excel的后缀为.xls, matlab是无法识别的, 需要将其另存为.xlsx文件格式 打开excel, 点击save as, 选中保存的文件格式是.xlsx即可
- AngularJS实现 购物车
<!DOCTYPE html> <html> <head> <meta charset = "utf-8"> <script ...
- 《C#高效编程》读书笔记06-理解几个等同性判断之间的关系
当创建自定义类型时(无论是class还是struct),应为类型定义"等同性"的含义.C#提供了4种不同的函数来判断两个对象是否"相等": public sta ...
- Mavlink 协议 理解
来源:blog.csdn.net/super_mice/article/details/44836585 之前看了mavlink协议,网上关于mavlink的资料不多.本文大概总结了下对mavlink ...
- 从零开始的全栈工程师——js篇2.16
js操作css样式 div.style.width=“200px” 在div标签内我们添加了一个style属性 并设定了width值 这种写法会给标签带来了大量的style属性 跟实际项目是不符的 我 ...
- H5如何做手机app(移动Web App)?图片轮播?ionic、MUI
移动Web App 跨平台开发 用户不需要去卖场来下载安装App 任何时候都可以发布App只需要一个开发项目 可以使用HTML5,CSS3以及JavaScript以及服务器端语言来完成(PHP,Rub ...
- Angular CLI的简单使用(1)
参考地址: https://v2.angular.cn/docs/ts/latest/cli-quickstart.html Angular CLI是一个命令行界面工具,它可以创建项目.添加文件以及 ...
- 报错:LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
参考原文:http://bbs.csdn.net/topics/390121452 项目>属性>配置属性>清单工具>输入和输出>嵌入清单:原来是“是”,改成“否” 如果上 ...