Train Problem I
来源:互联网 发布:c语言合法的字符常量 编辑:程序博客网 时间:2024/05/16 05:30
Train Problem I
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 17 Accepted Submission(s) : 9
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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.FINISH
Hint
Hint
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.".
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.".
#include <iostream>#include <stack>using namespace std;typedef stack<char> STA;int main(){STA s;int n, i, j, k, t[100];char str1[100], str2[100];while(scanf("%d%s%s", &n, str1, str2) != EOF){i = 0, j = 0, k = 1;s.push(str1[0]);t[0] = 1;while(i < n && j < n){if(s.size() && s.top() == str2[j])//栈不为空且栈顶元素跟目标数组相同,则弹出{s.pop();t[k++] = 0;//出j++;}else{if(i == n)break;s.push(str1[++i]);t[k++] = 1;//入}}if(i == n)//相对j,执行的都是弹出,也就是不能符合cout<<"No."<<endl;else{cout<<"Yes."<<endl;for(i=0; i<k; i++){ if(t[i]) cout<<"in"<<endl; else cout<<"out"<<endl;}}cout<<"FINISH"<<endl;}return 0;}
相关链接:三十分钟掌握STL
相关链接:STL的vector详解
- 1022 Train Problem I
- HDU1022 Train Problem I
- Train Problem I
- 1022:Train Problem I
- 1022 Train Problem I
- HDU1022 Train Problem I
- Train Problem I
- HDU1022:Train Problem I
- HDU1022--Train Problem I
- Train Problem I
- hdu1022-Train Problem I
- HDU1022 Train Problem I
- hdu1022 Train Problem I
- Train Problem I
- Train Problem I
- Train Problem I
- D. Train Problem I
- Train Problem I
- 最长公共子序列
- hdu4630 No Pain No Game(离线 线段树)
- MySQL左连接、右连接
- nyoj 54-小明的存钱计划
- 字符串转换成double和float总结
- Train Problem I
- America:From Freedom to Fascism
- 国外IT技术网站地址大全
- 移植FreeModbus+ModbusMaster(主机)+STM32至RT-Thread(1、2阶段)
- Web版RSS阅读器(二)——使用dTree树形加载rss订阅分组列表
- 键盘输入数字,对换前后5个元素
- ajax上传文件函数解析
- ADO.NET Entity Framework
- windows和linux双系统调整分区大小