HDUOJ 1022 (栈的基本应用)火车进站问题I
2015-07-29 14:15
447 查看
HDUOJ 1022 (栈的基本应用)火车进站问题 I
Train Problem I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25997 Accepted Submission(s): 9820
[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.
![](https://oscdn.geek-share.com/Uploads/Images/Content/201706/e07a7819adc3fce5ee075d011a0a1d3a.jpg)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201706/3ed5e24625225c0a75f84ac6dbeceabe.jpg)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201706/b72889e34d63c9ba1c9624e8d50888c9.jpg)
[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
HintHint
For the first Sample Input, we let train 1 get in, then train 2 and train 3.
So now train 3 is at the top of the railway, so train 3 can leave first, then train 2 and train 1.
In the second Sample input, we should let train 3 leave first, so we have to let train 1 get in, then train 2 and train 3.
Now we can let train 3 leave.
But after that we can't let train 1 leave before train 2, because train 2 is at the top of the railway at the moment.
So we output "No.".
(样例过少,容易对数据产生误解)
样例补充:
input
3 123 231
output
Yes.
in
in
out
in
out
out
My solution:
/*2015.7.26*/
/*注意事项:该题用了c++中的stack成员函数
c++ stl栈stack的成员函数介绍
操作 比较和分配堆栈
empty() 堆栈为空则返回真
pop() 移除栈顶元素
push() 在栈顶增加元素
size() 返回栈中元素数目
top() 返回栈顶元素
*/
/*刚开始用c提交时,总是提示编译错误,后来才注意到,stack是在c++中*/
/*栈的特点:先进后出*/
Train Problem I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25997 Accepted Submission(s): 9820
[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.
![](https://oscdn.geek-share.com/Uploads/Images/Content/201706/e07a7819adc3fce5ee075d011a0a1d3a.jpg)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201706/3ed5e24625225c0a75f84ac6dbeceabe.jpg)
![](https://oscdn.geek-share.com/Uploads/Images/Content/201706/b72889e34d63c9ba1c9624e8d50888c9.jpg)
[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
HintHint
For the first Sample Input, we let train 1 get in, then train 2 and train 3.
So now train 3 is at the top of the railway, so train 3 can leave first, then train 2 and train 1.
In the second Sample input, we should let train 3 leave first, so we have to let train 1 get in, then train 2 and train 3.
Now we can let train 3 leave.
But after that we can't let train 1 leave before train 2, because train 2 is at the top of the railway at the moment.
So we output "No.".
(样例过少,容易对数据产生误解)
样例补充:
input
3 123 231
output
Yes.
in
in
out
in
out
out
My solution:
/*2015.7.26*/
/*注意事项:该题用了c++中的stack成员函数
c++ stl栈stack的成员函数介绍
操作 比较和分配堆栈
empty() 堆栈为空则返回真
pop() 移除栈顶元素
push() 在栈顶增加元素
size() 返回栈中元素数目
top() 返回栈顶元素
*/
/*刚开始用c提交时,总是提示编译错误,后来才注意到,stack是在c++中*/
/*栈的特点:先进后出*/
#include<stdio.h> #include<string.h> #include<stack> #include<iostream> using namespace std; int main() { int low1,low2,t1,t2,i,j,k,n,c[12],num; char a[12],b[12]; while(scanf("%d",&n)!=EOF) { getchar(); num=1; memset(c,0,sizeof(c));/*bac数组全部赋值为0*/ low1=0; /*用来记录数组a的下标*/ low2=0;/*用来记录数组b的下标*/ //gets(a);/*gets()函数输入字符串时,以回车键结束,空格会被当作字符串记录*/ //gets(b); scanf("%s%s",a,b);/*当遇到空格结束字符串输入*/ t1=strlen(a); t2=strlen(b); stack<char>p; /*栈的定义*/ p.push(a[low1]);// c[num++]=1;// if(t1==t2) { while(low1<t1&&low2<t2) { if(p.empty()==1) { /*数组c用来记录火车的驶入和驶出,驶入为1,驶出为0(不需输入,已预先置0);*/ c[num]=1; // if(num==1) p.push(a[low1]);else p.push(a[++low1]); p.push(a[++low1]);// /*上面的这条语句等效于那些后面有“//”标号的语句*/ } else { if(p.top()==b[low2]) { p.pop(); low2++; } else { low1++; p.push(a[low1]); c[num]=1; } } num++; } if(p.empty()==1) { printf("Yes.\n"); for(i=1;i<num;i++) if(c[i]==1) printf("in\n"); else printf("out\n"); } else printf("No.\n"); } else printf("No.\n"); printf("FINISH\n"); } return 0; }
相关文章推荐
- eclipse下MAVEN离线环境搭建
- Android开发中实用工具之--Log工具类
- linux服务器间的拷贝命令
- 重要的事情说三遍!
- win7系统c盘瘦身,去虚拟内存方式
- 查询多个进程的目录和端口
- Win10显卡驱动如何更新?Win10显卡驱动更新方法介绍
- 查看 SELinux状态及关闭SELinux
- 照片墙翻转
- SQL2008创建定时作业以及常见问题
- angular中获取一个dom元素的作用域
- 修改Mongodb数据类型
- mysql修改密码
- eclipse中导入其他的web工程遇到和解决的问题
- Android中app卡顿原因分析示例
- git 版本回退总结
- 如何将character_set_database latin1 改为 gbk
- Android Studio 发生 Error:Execution failed for task ':app:packageDebug'.
- UVA 12433 Rent a Car
- 列表[List]基本操作