Train Problem I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25879 Accepted Submission(s): 9771



3 123 321 3 123 312
Yes. in in in out out out FINISH No. 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<stdio.h>
#define max 20
char in[max],out[max],target[max],stack[max];//进入顺序 步骤 目标顺序 栈 当前在车站里的火车
int main()
{
int n,top,nowpos,head,step;//top栈顶 nowpos达成目标进度 head车队头 step 步骤数
while(~scanf("%d",&n))
{
scanf("%s %s",in,target);
top=nowpos=head=step=0;
while(head<n)
{
stack[++top]=in[head++];//stack[1]=int[0]先把车一个个进栈
out[step++]='1';//步骤进去为1出去为0
while(top&&stack[top]==target[nowpos])//循环直到车一个个进栈,最后一辆车的编号和出去第一辆车编号相同进入循环
{
top--;//出去一辆栈顶-1
nowpos++;//完成进度加1
out[step++]='0';//出去的步骤加1;
}
}
if(nowpos==n)//如果完成的进度数等于n则可以完成过程
{
printf("Yes.\n");
for(int i=0;i<step;i++)
{
if(out[i]=='0')
printf("out\n");
else
printf("in\n");
}
printf("FINISH\n");
}
else
printf("No.\nFINISH\n");
}
return 0;
}
467



被折叠的 条评论
为什么被折叠?



