Train Problem I
http://acm.hdu.edu.cn/showproblem.php?pid=1022
Memory Limit: 65536/32768 K (Java/Others)
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
使用栈或者数组实现,用一个指标记录应该出来的火车的编号,然后检查进来的火车,如果相同就出去并检查栈内的火车,不同就压入栈。
完整代码:
/*15ms,228KB*/
#include <cstdio>
#include <cstring>
int n, top, t, h;
char in[10], out[10], s[10], d[10];
int main(void)
{
while (~scanf("%d", &n))
{
top = 0;
t = h = 0;
scanf("%s%s", in, out);
for (int i = 0, j = 0; i < n; i++)
{
s[top++] = in[i];
d[t++] = 1;
while (top > 0 && s[top - 1] == out[j])
top--, j++, d[t++] = 0;
}
if (top == 0)
{
puts("Yes.");
for (int i = 0; i < t; i++)
puts(d[i] == 0 ? "out" : "in");
}
else
puts("No.");
puts("FINISH");
}
return 0;
}
1203



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



