这题我的想法使用栈的思想去解决的,就是根据进站的序列加入栈中,若和出站最开始的序号相同,则出栈;然后继续进行此操作,和下一个出站的火车序号相比较。这样操作完后看看栈是否为空,就可以判断了。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std;
int main()
{
char s1[10],s2[10],s3[20][5];
int n;
while(scanf("%d%s%s",&n,s1,s2)!=EOF)
{
stack<char> s;
int top2=0,top3=0;
for(int i=0;i<n;i++)
{
s.push(s1[i]);
strcpy(s3[top3++],"in");//此处就是我的思想
while(!s.empty())
{
char a=s.top();
if(a==s2[top2])
{
strcpy(s3[top3++],"out");
s.pop();
top2++;
}
else
break;
}
}
if(s.empty())
{
printf("Yes.\n");
for(int i=0;i<top3;i++)
printf("%s\n",s3[i]);
}
else
printf("No.\n");
printf("FINISH\n");
}
return 0;
}