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个字符串链接后形成正确的括号序列的方案数(每个串都可以重复使用)(像'()()'和'(())'这样的都是合法的,像')( ...
随机推荐
- laravel captcha
https://packagist.org/packages/gregwar/captcha
- 【ufldl tutorial】Convolution and Pooling
卷积的实现: 对于每幅图像,每个filter,首先从W中取出对应的filter: filter = squeeze(W(:,:,filterNum)); 接下来startercode里面将filter ...
- hiho1096_divided_product
题目 给出两个正整数N和M, N <= 100, M <= 50, 可以将N分解成若干个不相等的正整数A1, A2... Ak的和,且A1, A2 ... Ak的乘积为M的倍数.即 N = ...
- json方法
http://penghuaiyi.iteye.com/blog/1922632 package com.yd.web.util; import java.lang.reflect.Type; imp ...
- 易通电脑锁2007V6.3.3.3无法卸载问题解决办法
易通电脑锁2007V6.3.3.3无法卸载问题解决办法把原版文件拷贝回去.bat@echo offcolor 2Fecho 该批处理会把易通电脑锁2007版原文件拷贝回去,解决易通电脑锁卸载时出现的运 ...
- windows 下使用 Filezilla server 搭建 ftp 服务器
windows 下使用 Filezilla server 搭建 ftp 服务器 1. Filezilla server 免费,开源, ftp 服务端 2. 下载安装, windows https:/ ...
- 【转】实现RTP协议的H.264视频传输系统
1. 引言 随着信息产业的发展,人们对信息资源的要求已经逐渐由文字和图片过渡到音频和视频,并越来越强调获取资源的实时性和互动性.但人们又面临着另外一种不可避免的尴尬,就是在网络上看到生动 ...
- CSS小结
一.1. css必须写在<head></head>里面的<style></style>里面 2. css 由选择器 + 规则组成, 规则由属性和值组成 ...
- WLAN频段介绍-04
ISM频段 ISM频段,此频段主要是开放给工业.科学.医学三个主要机构使用,该频段是依据美国联邦通讯委员会(FCC)所定义出来,并没有所谓使用授权的限制. 工业频段:美国频段为902-928MHz,欧 ...
- java synchronized静态同步方法与非静态同步方法,同步语句块
摘自:http://topmanopensource.iteye.com/blog/1738178 进行多线程编程,同步控制是非常重要的,而同步控制就涉及到了锁. 对代码进行同步控制我们可以选择同步方 ...