


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
给你一些火车,以数字来进行标号,判断以一的方式进站,是否能以二的方式出站。假如1 2 3 进站 则可以 3 2 1 出站。 假如1 3 2 进站 也可以 1 2 3出站,因为1进去后可以先出来。然后 2 3 进。 3 2 出。栈主要就是先进后出的思想。
上代码、
#include <stdio.h>
#include <stack>
#include <string.h>
#include <algorithm>
using namespace std;
int main()
{
int i,j,wbx;
int vis[500];
char s1[1000];
char s2[1000];
stack<char>s; //将char 类型的放进队列。
int t;
while(~scanf("%d%s%s",&t,s1,s2))
{
i=j=wbx=0;
while(!s.empty())
s.pop(); // 清空栈。
while(i<t)
{
s.push(s1[i++]); // 开始进站。
vis[wbx++]=1; // 将进站的in存入数组,令为一。
while(!s.empty() &&s.top()==s2[j])// 如果栈不空的话,并且栈首元素等于出站元素。则弹栈。
{
j++;
vis[wbx++]=0;// 出站的out也存入数组,令为0。
s.pop();// 弹栈。
}
}
if(!s.empty())
printf("No.\n");// 上面做好之后,如果此时栈还不是空的,则不能以方式2的顺序出站。
else //反之可以。
{
printf("Yes.\n");
for(i=0;i<wbx;i++)
{
if(vis[i])
printf("in\n");
else
printf("out\n");
}
}
printf("FINISH\n");// 完成
}
return 0;
}
原文地址:http://blog.csdn.net/sky_miange/article/details/43112859