http://acm.hdu.edu.cn/showproblem.php?pid=1022
Train Problem I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20606 Accepted Submission(s): 7753
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>
#include<string.h>
int main()
{
char IN[1000],OUT[1000],stack[1000];
int a[2000],N,i,j,k;
while(scanf("%d %s %s",&N,IN,OUT)!=EOF)
{
int top=-1;
for(i=0,j=0,k=0;i<=N&&j<N;)
{
if(stack[top]==OUT[j]&&top!=-1)
{
top-=1;
j+=1;
a[k]=0;
k+=1;
// printf("%d#\n",top);
}
else
{
top+=1;
stack[top]=IN[i];
i+=1;
a[k]=1;
k+=1;
}
}
if(top==-1)
{
printf("Yes.\n");
for(i=0;i<2*N;i++)
if(a[i]==0)
printf("out\n");
else
printf("in\n");
}
else
printf("No.\n");
printf("FINISH\n");
}
return 0;
}
457



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



