栈之火车进站和出站
![](http://acm.hdu.edu.cn/data/images/1022-1.jpg)
![](http://acm.hdu.edu.cn/data/images/1022-2.jpg)
![](http://acm.hdu.edu.cn/data/images/1022-3.jpg)
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stack>
using namespace std;
int main(void)
{
stack<char>s1;
stack<char>s2;
int n,i,j,t,a[1000];
char c[100010],d[100010];
while(scanf("%d",&n)!=EOF)
{j=0;
scanf("%s",c);
scanf("%s",d);
for(i=n-1;i>=0;i--)
s1.push(d[i]);
for(i=0;i<n;i++)
{
s2.push(c[i]);
a[j++]=1;
while(!s2.empty()&&s1.top()==s2.top())
{
s1.pop();
s2.pop();
a[j++]=0;
}
}
if(s1.empty()&&s2.empty())
{
printf("Yes.\n");
for(i=0;i<2*n;i++)
{
if(a[i]==1) printf("in\n");
else printf("out\n");
}
}
else printf("No.\n");
printf("FINISH\n");
}
return 0;
} 答案错误
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stack>
using namespace std;
int main(void)
{
stack<char>s1;
stack<char>s2;
int n,i,j,t,a[1000];
char c[100010],d[100010];
while(scanf("%d",&n)!=EOF)
{j=0;
scanf("%s",c);
scanf("%s",d);
for(i=n-1;i>=0;i--)
s1.push(d[i]);
for(i=0;i<n;i++)
{
s2.push(c[i]);
a[j++]=1;
while(!s2.empty()&&s1.top()==s2.top())
{
s1.pop();
s2.pop();
a[j++]=0;
}
}
if(s1.empty()&&s2.empty())
{
printf("Yes.\n");
for(i=0;i<2*n;i++)
{
if(a[i]==1) printf("in\n");
else printf("out\n");
}
}
else
{
printf("No.\n");
while(!s1.empty())
s1.pop(); 如果不能实现 ,那也就是说两个栈没有清空 ,必须清空才能继续下一个实例;
while(!s2.empty())
s2.pop();
}
printf("FINISH\n");
}
return 0;
}