1022 Train Problem 1 栈的运用
来源:互联网 发布:淘宝网上买的药可靠吗 编辑:程序博客网 时间:2024/06/18 12:57
Train Problem I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12900 Accepted Submission(s): 4724
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.
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 3213 123 312
Sample Output
Yes.inininoutoutoutFINISHNo.FINISHFor 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.".HintHint
#include <iostream>#include <stack>//重要方法#include <string>using namespace std;int main(){ int n,i; int p,q; while(cin>>n) { p=q=0; int inorout[20]; string ins,outs; cin>>ins>>outs; stack<char>train; for(i=0;i<n;i++) { train.push(ins[i]); inorout[p++]=1; while(!train.empty()&&train.top()==outs[q]) { q++; train.pop(); inorout[p++]=0; } } if(train.empty()) { cout<<"Yes."<<endl; for(i=0;i<2*n;i++) { if(inorout[i]==1) cout<<"in"<<endl; else cout<<"out"<<endl; } cout<<"FINISH"<<endl; } else { cout<<"No."<<endl; cout<<"FINISH"<<endl; } } return 0;}
- 1022 Train Problem 1 栈的运用
- Train Problem I hdu 1022 栈的运用
- HD 1022 Train Problem I 栈的运用 比较好理解的代码
- 杭电1022(栈的运用) 之 Train Problem I
- hdoj 1022 Train problem 1【栈 的理解】
- hdu 1022 Train Problem I(栈的应用。。。)
- hdu 1022 Train Problem I STL--栈的应
- hdu 1022 Train Problem I(栈的应用)
- hdu 1022 Train Problem I (栈的简单应用)
- [ACM] hdu 1022 Train Problem I(栈的使用)
- HDU 1022 Train Problem I(栈的应用)
- hdu 1022 Train Problem I (栈的应用)
- hdu 1022 Train Problem I_栈的应用经典例题
- hdu 1022 Train Problem I(栈的应用+STL)
- HDOJ题目1022Train Problem I(栈的模拟)
- HDU 1022 Train Problem I 栈的模拟
- HDU 1022 Train Problem I(栈的简单应用)
- hdu 1022 Train Problem I (栈的应用)
- 设计模式(十六)----- 迭代器模式(Iterator)----(JAVA版)
- Who do you think is the best coder, and why?
- HTML001——meta标签
- Android之sensor 传感器
- 安全卫士第四天--23黑名单拦截
- 1022 Train Problem 1 栈的运用
- 面相对象设计中组合与聚合详解
- 原创分析:iOS 中使用 Mobile Installation 安装 IPA(使用iTunes/AppStore一样的安装API)
- 安全卫士第四天--23拦截响一声的电话功能
- ctags找不到pthread相关函数
- 无符号数加减乘除
- C++单例模式详解
- linux下如何查看一个命令的安装路径
- Android之animation