栈

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

 

Description

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, () and [] 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

对于一个初学者,对栈的使用会有疑惑,什么是栈?怎么用它?很多书上解释的并不清楚。

简单说一下:

栈 (stack)又称堆栈,是一种受限制的线性表,其限制是只允许在表的一端进行插入和删除。

允许操作的一端称为栈顶(top),不允许 操作的称为栈底(bottom),每每次删除的数据元素总是最后插入的数据元素,所以栈又称为“后入先出表”,这是和队列的区别。

栈的储存结构有2种:一种顺序储存结构(顺序栈),一种链式储存结构(链式栈)。

今天主要来看看如何实现一个栈的功能

首先,栈的基本功能有:

1.       empty 判断堆栈是否为空

2.       pop   向堆栈里面压入一个数据

3.       push        向堆栈压入一个数据

4.       size          返回当前堆栈长度(即内部数据个数)

5.       top           得到堆栈栈顶数据

此题题意:

输入一个包含()和[]的括号序列,判断是否合法。

具体递归定义如下:1.空串合法;2.如果A和B都合法,则AB合法;3.如果A合法则(A)和[A]都合法。 

注意输入可能有空串

直接用栈

#include<iostream>
#include<stack>
#include<cstring>
#include<cstdio>
using namespace std; bool judge(char a,char b)
{
  if(a=='['&&b==']')return ;
  if(a=='('&&b==')')return ;
  return ;
} bool left(char a)
{
  if(a=='['||a=='(')return ;
  return ;
} int main()
{
  int cas;
  char s[];
  cin>>cas;
  getchar();
while(cas--)
{
stack<char>q;
gets(s);
  if(strcmp(s,"\n")==)
{
  cout<<"Yes"<<endl;
  continue;
}
  for(int i=;s[i];i++)
{
  if(q.empty())
{
    q.push(s[i]);
}
  else if(!judge(q.top(),s[i]))
{
  if(left(s[i]))
    q.push(s[i]);
}
  else     q.pop();
}
  if(q.empty())     cout<<"Yes"<<endl;
  else     cout<<"No"<<endl;
}
  return ;
}

我自己的是下面这一种,可能更容易理解一些,也是栈的思想

#include<stdio.h>
#include<string.h>
int main()
{
int i,j,k,T;
int flag;
int s[],st,top;
char c[];
scanf("%d",&T);
getchar();
while(T--)
{
fgets(c,sizeof(c),stdin);
flag=;
j=;
for(i=; c[i]!='\n'; i++)
{
if(c[i]=='(')
s[j++]=;
else if(c[i]=='[')
s[j++]=;
else if(c[i]==')')
{
if(j!= && s[j-]==)
j--;
else
{
flag=;
break;
}
}
else if(c[i]==']')
{
if(j!= && s[j-]==)
j--;
else
{
flag=;
break;
}
}
else
{
flag=;
break;
}
}
if(flag==|| j!=)
printf("No\n");
else
printf("Yes\n");
}
return ;
}

看了别人代码加我百度才明白栈的用法,发现要学的好多。。

UVa 673 Parentheses Balance(栈的使用)的更多相关文章

  1. UVA 673 Parentheses Balance (栈)

    题意描述: 给出一段只包含()和[]的字符串,判断是否合法,合法输出YES,不合法输出NO 规则: 1.该串为空,则合法 2.若A合法,B合法,则AB合法 3.若A合法,则(A)和[A]均合法 解题思 ...

  2. UVa 673 Parentheses Balance -SilverN

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

  3. UVa 673 Parentheses Balance

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

  4. UVa 673 Parentheses Balance【栈】

    题意:输入一个包含"()"和"[]"的序列,判断是否合法 用栈来模拟,遇到"(",“[”就入栈,遇到')',']'就取出栈顶元素看是否匹配, ...

  5. UVa 673 Parentheses Balance (stack)

    题目描述 : 判断字符串是不是符合正确的表达式形式. 要点 : 考虑字符串为空的时候,用getline输入,每一次判断后如果为No则要清空栈.对称思想. 注意输入格式. 代码: #include &l ...

  6. 【UVA】673 Parentheses Balance(栈处理表达式)

    题目 题目     分析 写了个平淡无奇的栈处理表达式,在WA了5发后发现,我没处理空串,,,,(或者说鲁棒性差?     代码 #include <bits/stdc++.h> usin ...

  7. uva673 - Parentheses Balance(栈)

    题意:1.空串合法.2.若A和B合法,则AB合法.3.若A合法,则(A)和[A]合法. 思路:遍历串,遇到(或[,则压入队列,若遇到),判断:若栈空,则不合法:若栈顶元素不是(,也不合法.]同理.因为 ...

  8. UVa673 Parentheses Balance

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

  9. UVA 673 (13.08.17)

     Parentheses Balance  You are given a string consisting of parentheses () and []. Astring of this ty ...

随机推荐

  1. Cyclic Nacklace - HDU 3746(next求循环节)

    题目大意:给你一些串,问如果想让这个串里面的循环节至少循环两次,需要添加几个字符(只能在最前面或者最后面添加).比如ababc 需要添加5个就是添加ababc. 分析:其实字符串的长度len-next ...

  2. 深入了解JavaScript中的for循环

    在ECMAScript5中,有三种for循环,分别是: 简单for循环 for-in forEach 在ES6中,新增了一种循环 for-of 简单for循环 const arr = [1, 2, 3 ...

  3. Android 4.4(KitKat)中VSync信号的虚拟化

    原文地址:http://blog.csdn.net/jinzhuojun/article/details/17293325 Android 4.1(Jelly Bean)引入了Vsync(Vertic ...

  4. umlの用例图

    我的总结是在看完uml学习之后又參考了同学借的<uml和ooad高速入门>的思路,利用齿轮带动的原理进行.废话不多说了.首先分析一下类图和用例图的联系与差别. 类图 用例图 类class ...

  5. Tomcatserverhttps协议配置简单介绍

    一. 数字签名证书制作 1. 用jdk自带的keytool工具生成证书. 2. 导出证书: 3. 交给CA签名认证: 注意:制作具体步骤演示样例參见附录. 二.改动server.xml文件 改动con ...

  6. lnux内核的malloc实现(Oracle的cache buffer影子)

    lnux内核的malloc实现(Oracle的cache buffer影子) 本文原创为freas_1990,转载请标明出处:http://blog.csdn.net/freas_1990/artic ...

  7. QT的信号与槽机制介绍

      信号与槽作为QT的核心机制在QT编程中有着广泛的应用,本文介绍了信号与槽的一些基本概念.元对象工具以及在实际使用过程中应注意的一些问题. QT是一个跨平台的C++ GUI应用构架,它提供了丰富的窗 ...

  8. TCP/IP协议原理与应用笔记09:数据通信---封装

    2016-08-091. 数据通信----封装: 2. 协议数据单元: PDU:对等层数据通信的单元. 比如Source端的应用层 和 Destination端的应用层是对等层(L7),这个时候L7 ...

  9. HDU 5211 筛法求约数

    给出n个数a1,a2...an,定义函数 f[i]=j,(i<j),表示aj mod ai=0 的最小j,其中j大于i,如果不存在这样的数,则f[i]=0 求n个数所有f[]值的和 先用筛法o( ...

  10. PID204 / 特种部队

    /* 双向DP 两条路 f[i][j] 表示第一条路末位置为i 第二条路末位置为j 的最优解 转移:对于下一个点 k=max(i,j)+1 可以更新 路1的末位置 也可以更新路2的末位置 f[i][k ...