stack栈
2015-07-27 10:36
302 查看
c++ stack(堆栈)是一个容器的改编,它实现了一个先进后出的数据结构(FILO)
使用该容器时需要包含#include<stack>头文件;
定义stack对象的示例代码如下:
stack<int>s1;
stack<string>s2;
stack的基本操作有:
1.入栈:如s.push(x);
2.出栈:如 s.pop().注意:出栈操作只是删除栈顶的元素,并不返回该元素。
3.返回栈顶数据:如s.top();
4.判断栈空:如s.empty().当栈空时返回true。
5.访问栈中的元素个数,如s.size();
6.stack<Elem>s:创建一个空的stack;
Total Submission(s): 25760 Accepted Submission(s): 9727
[align=left]Problem Description[/align]
As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes
a problem, there is only one railway where all the trains stop. So all the trains come in from one side and get out from the other side. For this problem, if train A gets into the railway first, and then train B gets into the railway before train A leaves,
train A can't leave until train B leaves. The pictures below figure out the problem. Now the problem for you is, there are at most 9 trains in the station, all the trains has an ID(numbered from 1 to n), the trains get into the railway in an order O1, your
task is to determine whether the trains can get out in an order O2.
[align=left]Input[/align]
The input contains several test cases. Each test case consists of an integer, the number of trains, and two strings, the order of the trains come in:O1, and the order of the trains leave:O2. The input is terminated by the end of file.
More details in the Sample Input.
[align=left]Output[/align]
The output contains a string "No." if you can't exchange O2 to O1, or you should output a line contains "Yes.", and then output your way in exchanging the order(you should output "in" for a train getting into the railway, and "out"
for a train getting out of the railway). Print a line contains "FINISH" after each test case. More details in the Sample Output.
[align=left]Sample Input[/align]
3 123 321
3 123 312
[align=left]Sample Output[/align]
Yes.
in
in
in
out
out
out
FINISH
No.
FINISH
难度:3
描述 现在,有一行括号序列,请你检查这行括号是否配对。
输入第一行输入一个数N(0<N<=100),表示有N组测试数据。后面的N行输入多组输入数据,每组输入数据都是一个字符串S(S的长度小于10000,且S不是空串),测试数据组数少于5组。数据保证S中只含有"[","]","(",")"四种字符输出每组输入数据的输出占一行,如果该字符串中所含的括号是配对的,则输出Yes,如果不配对则输出No样例输入
样例输出
使用该容器时需要包含#include<stack>头文件;
定义stack对象的示例代码如下:
stack<int>s1;
stack<string>s2;
stack的基本操作有:
1.入栈:如s.push(x);
2.出栈:如 s.pop().注意:出栈操作只是删除栈顶的元素,并不返回该元素。
3.返回栈顶数据:如s.top();
4.判断栈空:如s.empty().当栈空时返回true。
5.访问栈中的元素个数,如s.size();
6.stack<Elem>s:创建一个空的stack;
Train Problem I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25760 Accepted Submission(s): 9727
[align=left]Problem Description[/align]
As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes
a problem, there is only one railway where all the trains stop. So all the trains come in from one side and get out from the other side. For this problem, if train A gets into the railway first, and then train B gets into the railway before train A leaves,
train A can't leave until train B leaves. The pictures below figure out the problem. Now the problem for you is, there are at most 9 trains in the station, all the trains has an ID(numbered from 1 to n), the trains get into the railway in an order O1, your
task is to determine whether the trains can get out in an order O2.
[align=left]Input[/align]
The input contains several test cases. Each test case consists of an integer, the number of trains, and two strings, the order of the trains come in:O1, and the order of the trains leave:O2. The input is terminated by the end of file.
More details in the Sample Input.
[align=left]Output[/align]
The output contains a string "No." if you can't exchange O2 to O1, or you should output a line contains "Yes.", and then output your way in exchanging the order(you should output "in" for a train getting into the railway, and "out"
for a train getting out of the railway). Print a line contains "FINISH" after each test case. More details in the Sample Output.
[align=left]Sample Input[/align]
3 123 321
3 123 312
[align=left]Sample Output[/align]
Yes.
in
in
in
out
out
out
FINISH
No.
FINISH
#include<stdio.h> #include<iostream> #include<string.h> #include<stack> using namespace std; char b1[15],b2[15]; int c[15]; int main() { int n,i,j,m,k; while(scanf("%d %s %s",&n,&b1,&b2)!=EOF) { k=0; stack<char>a; memset(c,0,sizeof(c)); //a.push(b1[0]); for(i=0,j=0;i<n;i++) { a.push(b1[i]); c[k++]=1; while(a.size()!=0&&a.top()==b2[j]) { c[k++]=-1; a.pop(); ++j; } } if(j==n) { printf("Yes.\n"); for(i=0;i<k;i++) { if(c[i]==1) printf("in\n"); else printf("out\n"); } printf("FINISH\n"); } else printf("No.\nFINISH\n"); } return 0; }
括号配对问题
时间限制:3000 ms | 内存限制:65535 KB难度:3
描述 现在,有一行括号序列,请你检查这行括号是否配对。
输入第一行输入一个数N(0<N<=100),表示有N组测试数据。后面的N行输入多组输入数据,每组输入数据都是一个字符串S(S的长度小于10000,且S不是空串),测试数据组数少于5组。数据保证S中只含有"[","]","(",")"四种字符输出每组输入数据的输出占一行,如果该字符串中所含的括号是配对的,则输出Yes,如果不配对则输出No样例输入
3 [(]) (]) ([[]()])
样例输出
No No Yes
#include<stdio.h> #include<string.h> #include<iostream> #include<stack> using namespace std; char S[11000]; int main() { int a,b,i,top,c; scanf("%d",&a); getchar(); while(a--) { gets(S); b=strlen(S); if(b&1)//判断字符串长度是否为奇数 { printf("No\n"); continue; } else if(S[0]==')'||S[0]==']')//判断是否从右括号开始 { printf("No\n"); continue; } else { stack<char>g;//创建一个空的栈 g.push(S[0]); for(i=1;i<b;i++) { if(g.size()==0) g.push(S[i]);//要先判断栈里面有没有数据,要不然g.top()会出现错误 else{ if(g.top()=='('&&S[i]==')')//配对出栈 g.pop(); else if(g.top()=='['&&S[i]==']') g.pop(); else g.push(S[i]);} } if(g.empty()==1) printf("Yes\n"); else printf("No\n"); } } return 0; }
相关文章推荐
- 突破防盗链机制:使用referrer-killer
- 将图片转成二进制并生成Base64编码,可以在网页中通过url查看图片
- C#中的委托的简单使用
- Android高效加载大图、多图解决方案,有效避免程序OOM
- SVN Error:URL access forbidden for unknown reason
- MFC 多屏显示
- java多线程-专题-聊聊并发(四)深入分析ConcurrentHashMap
- 【进阶android】Volley源码分析——总述
- MySQL中group_concat函数在项目中的应用
- 分析apache日志,统计ip访问频次命令
- 突破防盗链机制:使用referrer-killer
- 纯 PHP 代码最好在文件末尾删除 PHP 结束标记
- Cocos2d-x中createEdgeBox()的各项参数的含义
- linux进程的介绍和管理
- linux进程的介绍和管理
- ios-UI-汤姆猫德游戏实现
- IDEA开发Spark1应用程序
- InnoDB buffer pool 刷新快慢取决因素
- iOS Sprite Kit教程之真机测试以及场景的添加与展示
- 委托简介