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个字符串链接后形成正确的括号序列的方案数(每个串都可以重复使用)(像'()()'和'(())'这样的都是合法的,像')( ...
随机推荐
- [css] 浏览器字体和css设置字体之间的关系
原文链接:http://www.zhangxinxu.com/wordpress/2010/06/%E5%8F%AF%E7%94%A8%E6%80%A7%E4%B9%8B%E6%B5%8F%E8%A7 ...
- js控制只能输入数字
onkeyup=clearNoNum(this) function clearNoNum(obj) { obj.value = obj.value.replace(/[^\d.]/g," ...
- 【服务器环境搭建-Centos】系统分区 待续
df命令查看,显示/dev/vda,而不是sda或hda ,为什么? 虚拟机为了提升性能,一般使用virtio作为磁盘驱动,在虚拟机里面磁盘会显示成vda而不是传统的sda,有什么办法可以让他显示成s ...
- 转 : c++ 结构体 前向声明
typedef struct tag_guid { ULONGLONG utime; ULONGLONG umac; }tpguid; class A { private: int m_teset1; ...
- jquery height
heigth -- 内容高度innerHeight -- 包含padding但不包含borderouterHeight -- 包含borderouterHeight(true) -- 包含margin ...
- js上下滚屏效果,代码通过测试
这是html代码 <div class="box"> <div class="bcon"> <h1><b>领号实 ...
- 例题:打印正三角形。两层for循环,难点明白行与列的关系
while (true) { string s = "★";//s代表五角星 string t = ...
- bower install和cnpm install
bower install :主要是安装前端的相关框架,如Boostarp,Angular等 cnpm install :主要是安装后端的,如express等
- C++继承与派生(原理归纳)
1. C++继承与java不同,java遵循单继承,但java的接口为其不足做了很好的弥补了. C++则是灵活的多,为多继承.即一个C++类可以同时继承N个类的属性. 2. 对于继承方式 : 有三 ...
- 下载模板,上传EXCEL
说道SAP里对EXCEL操作的大概就是上传,下载,显示了... 下载:(文档是通过SMW0上传的)注:如果下载的时候需要填充EXCEL的值,...请参考另一篇文档,OLE CALL METHOD CL ...