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;
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
Problem Description
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.
![](https://i-blog.csdnimg.cn/blog_migrate/e07a7819adc3fce5ee075d011a0a1d3a.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/3ed5e24625225c0a75f84ac6dbeceabe.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/b72889e34d63c9ba1c9624e8d50888c9.jpeg)
Input
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.
Output
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.
Sample Input
3 123 321 3 123 312
Sample Output
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; }
-
-