HDU 1022 栈的问题
来源:互联网 发布:花千骨网络流行语 编辑:程序博客网 时间:2024/05/17 07:30
2014-09-27 21:47 5人阅读 评论(0)收藏 举报
Train Problem I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 21182 Accepted Submission(s): 8015
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.".直接是考察栈的输入输出问题。思路:每次先找out序列,然后根据out序列按照in序列进栈。如果栈顶跟out吻合,那么就可以出栈。在每次进栈出栈都要记录是进还是出!HintHint#include <stdio.h>#include <iostream>#include <stack>#include <string.h>#include <string>using namespace std;int main(){ stack<int> s; string in,out; int buf[10]; int size1,size2,n; int pri[25]; //1表示进,2表示出 while(cin>>n) { size1=size2=0; cin>>in>>out; int flag=0; if(s.empty()!=true)//清栈 s.pop(); for(int k=0;k<10;++k) buf[k]=0; for (int p=0;p<25;++p) pri[p]=0; for(int i=0;i<n;++i) //out { if(buf[out[i]-'0']==0) { for(int j=0;j<n;++j) //in 如果没有进栈,那么就得按照顺序一直进栈,直到我们需要的出栈位出现 { if(in[j]!=out[i] && buf[in[j]-'0']==0) { s.push(in[j]-'0'); pri[size2++]=1; buf[in[j]-'0']=1; } else if(buf[in[j]-'0']==0) { s.push(in[j]-'0'); pri[size2++]=1; //pri[size2++]=2; buf[in[j]-'0']=1; break; } } } if(buf[out[i]-'0']==1)//说明已经进站了 { if(s.top()==out[i]-'0') //栈顶 { s.pop(); pri[size2++]=2; } else //不在栈顶那么顺序不能输出 { flag=1; } if(flag==1) break; } } if(flag==1) cout<<"No."<<endl<<"FINISH"<<endl; else { cout<<"Yes."<<endl; for(int l=0;l<size2;++l) { if(pri[l]==1) cout<<"in"<<endl; else cout<<"out"<<endl; } cout<<"FINISH"<<endl; } } return 0;}
0 0
- HDU 1022 栈的问题
- HDU 1022 栈的问题
- HDU 1022 (栈的问题)
- hdu 1022 火车 栈 问题
- 解决HDU爆栈的问题
- 解决HDU爆栈的问题 .
- 解决HDU爆栈的问题
- 解决HDU爆栈的问题
- 解决HDU爆栈的问题
- 解决HDU爆栈的问题
- HDU-1022 火车进出站问题【栈】
- hdu-1022 栈的应用。
- hdu 1022 栈的应用
- HDU-1258(DFS搜索的问题)
- hdu 2212 dfs 简单的思维问题
- hdu 1007 求最进点对的问题
- HDU 2602 最朴实的背包问题
- HDU 2546 饭卡 【隐藏的背包问题】
- 【JS】JS判断字符输入个数(数字英文长度记为1,中文记为2,超过长度自动截取)
- SHA1算法详解
- 11招教你如何玩转数据库设计(转)
- oracle的导入导出表
- java(29) - 线程详解
- HDU 1022 栈的问题
- Morning Walk - UVa 10596 欧拉回路
- jeecms框架单点登录功能的实现
- UVA201 Squares
- 1008. Elevator (20)
- 输出完数
- 批量插入SQL
- 算法生成太极八卦图
- 限制avd网速