Description



Input
Output
Sample Input
3 123 321 3 123 312
Sample Output
Yes. in in in out out out FINISH No. FINISH
Hint
Hint 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.".
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
int main()
{
int n,i,j,k;
char str1[10],str2[10];
int flag[20];//存储进栈出栈的状态,in为进栈,out为出栈
while(~scanf("%d",&n))
{
stack<char >q;
scanf("%s %s",str1,str2);
j=0;//记录进栈出栈的总次数
k=0;//记录出栈的次数
for(i=0;i<n;i++)
{
q.push(str1[i]);
flag[j++]=1;
while(!q.empty()&&q.top()==str2[k])//栈非空且栈的元素与str2的元素相同
{
flag[j++]=0;
q.pop();
k++;
}
}
if(q.empty())
{
printf("Yes.\n");
for(i=0;i<j;i++)
{
if(flag[i])
printf("in\n");
else
printf("out\n");
}
printf("FINISH\n");
}
else
{
printf("No.\n");
printf("FINISH\n");
}
}
return 0;
}
原文:http://blog.csdn.net/u013486414/article/details/41146913