hdoj 1022 Train Problem I
2015-06-17 13:04
253 查看
Train Problem I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 24980 Accepted Submission(s):
9424
[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.
[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
此题并不像题中测试数据一样好像是逆序输出即可,而是要用栈的思维,并不是说必须所有的车都进站了 才可以统一按顺序出来,而是说可以先进入一辆车然后这辆车出来,然后再进下一辆车,也可以先进两辆然后出来一辆再进一辆,即何时进站何时出站都可以(前提是符合题目的进出站要求即先进后出)
STL代码:
#include<stdio.h> #include<string.h> #include<stack> #define MAX 1100 using namespace std; int main() { int n,m,j,i,t,k; char jin[MAX],chu[MAX];//进展顺序和出站顺序数组 int vis[MAX];//记录第i次是进站还是出站 while(scanf("%d",&t)!=EOF) { scanf("%s %s",jin,chu); stack<char>s;//定义一个字符型的栈 memset(vis,0,sizeof(vis)); for(i=0,j=0,k=0;i<t;i++) { s.push(jin[i]); vis[k++]=1; while(!s.empty()&&s.top()==chu[j])//如果站不为空且栈顶的元素与出站顺序的元素相同 { j++;//删除已出的元素 vis[k++]=0;//出站并标记 s.pop();//删除栈首元素 } } if(s.empty()) { printf("Yes.\n"); for(i=0;i<k;i++) { if(vis[i]) printf("in\n"); else printf("out\n"); } } else printf("No.\n"); printf("FINISH\n"); } return 0; }
用数组实现:
#include<stdio.h> #include<string.h> #define MAX 1100 char zhan[MAX]; char jin[MAX],chu[MAX];//进展顺序和出站顺序数组 int vis[MAX];//记录第i次是进站还是出站 int main() { int n,m,j,i,s,sum,t,top,k; while(scanf("%d",&t)!=EOF) { scanf("%s %s",jin,chu); memset(vis,0,sizeof(vis)); top=0;k=0; for(i=0,j=0,k=0;i<t;i++) { zhan[top++]=jin[i];//存栈 vis[j++]=1; while(top>=0&&zhan[top-1]==chu[k])//如果站不为空且栈顶的元素与出站顺序的元素相同 { vis[j++]=0;//出站并标记 top--;//删除栈首元素 k++;//删除已出的元素 } } if(top<0) { printf("Yes.\n"); for(i=0;i<j-1;i++) { if(vis[i]) printf("in\n"); else printf("out\n"); } } else printf("No.\n"); printf("FINISH\n"); } return 0; }
相关文章推荐
- 【ORA-00060】 deadlock detected while waiting for resource
- 记AWSome Day Shanghai 150612
- Could not reliably determine the server's fully qualified domain name?
- HDU 1337 The Drunk Jailer--(模拟题找规律)
- 当摄像机被墙挡住后,墙消失,相反则出现(三维坐标到二维坐标的转换实例Camera.main.WorldToScreenPoint)
- raid简介
- http://daikainan.iteye.com/blog/1405575
- hdu3555 - Bomb(2010 ACM-ICPC Multi-University Training Contest(12))数位dp
- UNIX文件结构(转自UNIX/AIX操作系统基础教程)
- Cuda kernel failed. Error: invalid device function
- macbook air电池保养方法
- GoldenGate过程 abend,报错OGG-00868 ORA-02396: Exceeded Maximum Idle Time, Please Connect Again
- Pentest Box Tools List of Tools contained in PentestBox
- BMap API(Baidu)与google.maps API(Google)
- 事件传递之响应链(Event Delivery: The Responder Chain)【转】
- Zencart 1.5.X – login does not work – session / cookie problem – explained
- TurboMail防黑客入侵邮件系统骗取货款
- RAC连接时的2种方式Connect Time Failver和taf
- LeetCode——Contains Duplicate II
- Filter Chain in J2EE : revise response header