Problem A 栈
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, (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 <stack>
using namespace std;
int main()
{
int n;
cin >> n;
cin.get();
while (n--)
{
stack<char>st_ch;
int loge = 1;
char c;
while (cin.get(c) && c != '\n')
{
if (c == ')')
{
if (st_ch.empty())
loge = 0;
else if (st_ch.top() == '(')
st_ch.pop();
else
loge = 0;
}
else if (c == ']')
{
if (st_ch.empty())
loge = 0;
else if (st_ch.top() == '[')
st_ch.pop();
else
loge = 0;
}
else
st_ch.push(c);
}
if (st_ch.empty()&&loge)
cout << "Yes" << endl;
else
cout << "No" << endl; }
return 0; }
Problem A 栈的更多相关文章
- hdu Train Problem I(栈的简单应用)
Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...
- Train Problem I(栈)
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- train problem I (栈水题)
杭电1002http://acm.hdu.edu.cn/showproblem.php?pid=1022 Train Problem I Time Limit: 2000/1000 MS (Java/ ...
- Hdu 1022 Train Problem I 栈
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Train Problem(栈的应用)
Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of studen ...
- HDU1022 Train Problem I 栈的模拟
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1042 栈的模拟,题目大意是已知元素次序, 判断出栈次序是否合理. 需要考虑到各种情况, 分类处理. 常 ...
- Problem K 栈
Description A math instructor is too lazy to grade a question in the exam papers in which students a ...
- Problem D: 栈小游戏
#include <iostream> #include <vector> #include <stack> #include <algorithm> ...
- CF 990C. Bracket Sequences Concatenation Problem【栈/括号匹配】
[链接]:CF [题意]: 给出n个字符串,保证只包含'('和')',求从中取2个字符串链接后形成正确的括号序列的方案数(每个串都可以重复使用)(像'()()'和'(())'这样的都是合法的,像')( ...
随机推荐
- Android控件之AutoCompleteTextView(自动匹配输入的内容)
一.功能 动态匹配输入的内容,如百度搜索引擎当输入文本时,可以根据内容显示匹配的热门信息 二.独特属性 android:completionThreshold = "2" — ...
- Python Paramiko模块安装和使用
1.简介 大家会发现,常见的解决方法都会需要对远程服务器必要的配置,如果远程服务器只有一两台还好说,如果有N台,还需要逐台进行配置,或者需要使用代码进行以上操作时,上面的办法就不太方便了. 使用par ...
- STM8s窗口看门狗
看看窗口看门狗的框图 从图里看出产生复位信号有2个方式: 1 WDGCR寄存器的T6 由1变0,也就是从此寄存器的值从0x40变成0x3F会产生复位信号: 2 当寄存器WDGCR的值大于WDGWR的时 ...
- js setTimeout 与 setInterval 以及 for 循环 刷新UI
1. setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式:执行一次: 如果需要执行多次,自身再次调用 setTimeout(): 示例:无穷循环并带停止按钮的 <html&g ...
- Docker 使用指南 (二)—— 搭建本地仓库
版权声明:本文由田飞雨原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/94 来源:腾云阁 https://www.qclou ...
- 猫都能学会的Unity3D Shader入门指南
https://onevcat.com/2013/07/shader-tutorial-1/ https://onevcat.com/2013/08/shader-tutorial-2/
- 如何通过ildasm/ilasm修改assembly的IL代码
原文地址:http://kb.cnblogs.com/page/101162/ 这段时间为跟踪一个Bug而焦头烂额,最后发现是Framework的问题,这让人多少有些绝望.所以到微软论坛提了个帖子,希 ...
- Bootstrap标签
Bootstrap是Twitter推出的一个用于前端开发的开源工具包.用外链加载的方式可以将Bootstrap链接进来 常用方式: <link rel="stylesheet" ...
- HTML5 localStorage and sessionStorage
转载至:http://blog.csdn.net/fdipzone/article/details/25517615 HTML5 提供两种web存储方法,localStorage 与 sessionS ...
- Ubuntu配置Open BlockChain
/* 以前只是听说过BlockChain,没怎么了解过,最近导师在搞Blockchain,让我配一下Open BlockChain的环境,于是就硬着头皮配了一下,还挺顺利的. 由于没接触过BlockC ...