HDU-1022
OutputThe 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 321 3 123 312Sample Output
Yes.
in
in
in
out
out
out
FINISH
No.
FINISH
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.".
题解:用栈来判断,序列 1 元素进栈,如果栈非空且栈顶元素和序列 2 当前元素相同,出栈;否则,序列 1 元素继续进栈。过程中可以用数组记录火车是进栈还是出栈。
AC代码为:
#include<stdio.h>
#include<string.h>
int main()
{
int num,q,p,o,in[20],out[20];
char iin[20],oout[20];
while(scanf("%d%s%s",&num,iin,oout)!=EOF)
{
int flag[20]={0};
int station[20]={0};
for(int i=0;i<num;i++)
{
in[i]=iin[i]-'0';
out[i]=oout[i]-'0';
}
q=p=0;
o=0;
for(int i=0;i<num;i++)
{
station[q]=in[i];
q++;
o++;
while(station[q-1]==out[p])
{
q--;
p++;
flag[o]=1;
o++;
}
}
if(p==num)
{
printf("Yes.\n");
for(int i=0;i<o;i++)
{
if(flag[i]==0)
printf("in\n");
else
printf("out\n");
}
}
else
printf("No.\n");
printf("FINISH\n");
}
return 0;
}
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步