chapter6 数据结构基础之习题 Parentheses Balance
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<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
int compare(char p,char q)
{
if(p=='('&&q==')') return 1;
if(p=='['&&q==']') return 1;
else return 0;
}
int main()
{
int n,L,flag;
char c[100];
scanf("%d\n",&n);
for(int i=0; i<n; i++)
{
stack<char> st;
flag=0;
gets(c);
L=strlen(c);
for(int j=0; j<L; j++)
{
if(c[j]=='('||c[j]=='[') st.push(c[j]);
if(c[j]==')'||c[j]==']')
{
if(st.empty())
{
flag=1;
break;
}
if(compare(st.top(),c[j])) st.pop();
else{
flag=1;
break;
}
}
}
if(flag==1||!st.empty()) cout<<"No"<<endl;
else cout<<"Yes"<<endl;
}
return 0;
}
chapter6 数据结构基础之习题 Parentheses Balance的更多相关文章
- 算法与数据结构基础 - 分治法(Divide and Conquer)
分治法基础 分治法(Divide and Conquer)顾名思义,思想核心是将问题拆分为子问题,对子问题求解.最终合并结果,分治法用伪代码表示如下: function f(input x size ...
- 算法与数据结构基础 - 贪心(Greedy)
贪心基础 贪心(Greedy)常用于解决最优问题,以期通过某种策略获得一系列局部最优解.从而求得整体最优解. 贪心从局部最优角度考虑,只适用于具备无后效性的问题,即某个状态以前的过程不影响以后的状态. ...
- 算法与数据结构基础 - 深度优先搜索(DFS)
DFS基础 深度优先搜索(Depth First Search)是一种搜索思路,相比广度优先搜索(BFS),DFS对每一个分枝路径深入到不能再深入为止,其应用于树/图的遍历.嵌套关系处理.回溯等,可以 ...
- 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 ...
- UVa673 Parentheses Balance
// UVa673 Parentheses Balance // 题意:输入一个包含()和[]的括号序列,判断是否合法. // 具体递归定义如下:1.空串合法:2.如果A和B都合法,则AB合法:3.如 ...
- Python之数据结构基础
一.数据结构基础 a.什么是数据结构 b.数据结构的分类 c.列表 import random from timewrap import ...
- 数据结构基础(1)--数组C语言实现--动态内存分配
数据结构基础(1)--数组C语言实现--动态内存分配 基本思想:数组是最常用的数据结构,在内存中连续存储,可以静态初始化(int a[2]={1,2}),可以动态初始化 malloc(). 难点就是数 ...
- php数据结构课程---1、数据结构基础介绍(程序是什么)
php数据结构课程---1.数据结构基础介绍(程序是什么) 一.总结 一句话总结: 程序=数据结构+算法 设计好数据结构,程序就等于成功了一半. 数据结构是程序设计的基石. 1.数据的逻辑结构和物理结 ...
随机推荐
- ionic 创建某个文件下的page
ionic g page 文件名 --pagesDir src/pages/about
- 创建pod索引库(Specs)
专门用来存放xxx.podspec 的索引文件的库就叫做索引库.我们需要将这些索引文件上传到远程索引库才能保证其他的人能够拿来用. 创建一个远程索引库和本地索引库对应起来,步骤如下: 1.登录开源中国 ...
- mysql在windows上安装
一.在window上安装mysql MySQL是一个小巧玲珑但功能强大的数据库,目前十分流行.但是官网给出的安装包有两种格式,一个是msi格式,一个是zip格式的.很多人下了zip格式的解压发现没有s ...
- js for 循环 添加tr td 算法
StringBuffer sb=new StringBuffer(); int n = 5; sb.append("<tr>"); List<MenuBean&g ...
- python--(十步代码学会线程)
python--(十步代码学会线程) 一.线程的创建 Thread实例对象的方法 # isAlive(): 返回线程是否活动的. # getname(): 返回线程名. # setName(): 设置 ...
- 1066N !最右边非零数
http://hi.baidu.com/nicker2010/item/4fa83c4c5050b3e5a4c066ec 另一个 Last non-zero Digit in N! Time Limi ...
- 算是学完了《Servlet&JSP学习笔记》,立此存照
我感觉从构架上来说,算是入门了, 终于可以正式进入SPRING的学习啦...爽 代码就不弄了,真的太多了...花了差不多两周呢..
- npm install socket.io 提示缺少"VCBuild.exe"
http://www.cnblogs.com/yangzhx/p/4648501.html https://www.v2ex.com/t/120493
- python 执行环境
一些函数 执行其它非python程序 1 一些函数 callable callable()是一个布尔函数,确定一个对象是否可以通过函数操作符(())来调用.如果函数可调用便返回True,否则便是Fal ...
- UNP(一):网络编程角度下的TCP、UDP协议
此博文是学习UNP(UNIX Network Programming)后的读书笔记,供以后自己翻阅回想知识. TCP.UDP概述 在前面<计算机网络与TCP/IP>栏目下已经介绍过一些关于 ...