UVa673 Parentheses Balance
// UVa673 Parentheses Balance
// 题意:输入一个包含()和[]的括号序列,判断是否合法。
// 具体递归定义如下:1.空串合法;2.如果A和B都合法,则AB合法;3.如果A合法则(A)和[A]都合法。
// 算法:用一个栈。注意输入可能有空串
#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
#include<stack>
using namespace std; int main()
{
int n;
scanf("%d", &n); getchar();
char s[256];
while(n--)
{
gets(s);
// puts(s);
int len=strlen(s);
stack<char> st;
char c;
bool ok=true;
for(int i=0;i<len&&ok;i++)
{
switch(s[i])
{
case '(':
case '[':
st.push(s[i]);
break;
case ')':
case ']':
if(st.empty())
ok=false;
else
{
c=st.top(); st.pop();
if(s[i]==')' && c!='(')
ok=false;
else if(s[i]==']' && c!='[')
ok=false;
}
break;
}
}
if(ok&&st.empty())
printf("Yes\n");
else
printf("No\n"); } return 0;
}
UVa673 Parentheses Balance的更多相关文章
- uva673 - Parentheses Balance(栈)
题意:1.空串合法.2.若A和B合法,则AB合法.3.若A合法,则(A)和[A]合法. 思路:遍历串,遇到(或[,则压入队列,若遇到),判断:若栈空,则不合法:若栈顶元素不是(,也不合法.]同理.因为 ...
- UVA-673 Parentheses Balance(栈)
题目大意:给一个括号串,看是否匹配. 题目分析:一开始用区间DP写的,超时了... 注意:空串合法. 代码如下: # include<iostream> # include<cstd ...
- UVa 673 Parentheses Balance -SilverN
You are given a string consisting of parentheses () and []. A string of this type is said to be corr ...
- UVa 673 Parentheses Balance
一个匹配左右括号的问题 /*UVa 673 Parentheses Balance*/ #include<iostream> #include<algorithm> #incl ...
- 【习题 6-1 UVA-673】Parentheses Balance
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 括号匹配. 栈模拟就好. 多种括号也是一样可以做的. [代码] #include <bits/stdc++.h> usi ...
- UVa 673 Parentheses Balance(栈的使用)
栈 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description You are ...
- 平衡的括号 (Parentheses Balance UVA - 673)
题目描述: 原题:https://vjudge.net/problem/UVA-673 题目思路: 1.水题 2.栈+模拟 3.坑在有空串 AC代码 #include <iostream> ...
- 栈及其DFS:B - Parentheses Balance
解题心得及总结: 总结: 1.递推:又1推出n,数列中的基本到通项,最终目标得出通项公式. 递归:又n先压缩栈到1,再从函数的出口找到1,又1到n,再从n计算到1: 2.判断是否可以由递推或递推得出, ...
- chapter6 数据结构基础之习题 Parentheses Balance
You are given a string consisting of parentheses () and []. A string of this type is said to be corr ...
随机推荐
- 基于phpExcel写的excel类
<?php /* * 类的功能 * 传入二位数组导出excel * 传入excel 导出二位数组 * @author mrwu */ require('PHPExcel.php'); requi ...
- Mac下配置PHP+Apache+phpMyAdmin+MySql远程链接
最近的项目是微信公众号平台的开发,微信官方给出的Demo是PHP的,发现大部分的学习资料也是PHP,那好吧,放弃Java,来段儿PHP吧 下面说说Mac下搭建PHP环境 数据库:MySQL-5.6.2 ...
- Mysql 多表联合查询效率分析及优化
1. 多表连接类型 1. 笛卡尔积(交叉连接) 在MySQL中可以为CROSS JOIN或者省略CROSS即JOIN,或者使用',' 如: SELECT * FROM table1 CROSS JO ...
- C# Read/Write another Process' Memory z
http://www.codeproject.com/Articles/670373/Csharp-Read-Write-another-Process-Memory This article aim ...
- lightoj 1022
直接算即可,特别要注意精度 #include<cstdio> #include<cmath> int main(){ int t, CASE(0); double r; sca ...
- web自动化框架之一介绍与环境搭建(Selenium+Eclipse+Python)
看到一篇环境搭建文章,详细又全面,这里就不一一重复了 http://blog.csdn.net/dyllove98/article/details/9390649 其它: 1.框架介绍 整个 ...
- Hadoop对文本文件的快速全局排序
一.背景 Hadoop中实现了用于全局排序的InputSampler类和TotalOrderPartitioner类,调用示例是org.apache.hadoop.examples.Sort. 但是当 ...
- Codeforces Round #364
http://codeforces.com/contest/701 A - Cards 水 // #pragma comment(linker, "/STACK:102c000000,102 ...
- SQL中以count及sum为条件的查询
在开发时,我们经常会遇到以“累计(count)”或是“累加(sum)”为条件的查询.比如user_num表: id user num 1 a 3 2 a 4 3 b 5 4 b 7 例1:查询出现 ...
- Ubuntu 12.04 pppoe拨号问题
我的系统信息: Ubuntu 12.04.4 X64 Q001: 我学校需要使用pppoe拨号上网.我在宿舍架了个路由,可以使用无线连接拨号上网,也可以使用网线连接.在ubuntu下,使用无线连接时没 ...