#include<cstdio>
#include<stack>
#include<cstring>
using namespace std;
int main()
{
int i,a,b,t,o1[10],o2[10],n;
stack<int> c;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)
scanf("%d",&o1[i]);
for(i=1;i<=n;i++)
scanf("%d",&o2[i]);
a=1;
b=1;
t=1;
while(a<=n)
{
if(o2[a]==o1[b])
{
a++;
b++;
}
else if(!c.empty()&&c.top()==o2[a])
{
c.pop();
a++;
}
else if(b<=n)
{
c.push(o1[b++]);
}
else
{
t=0;
break;
}
}
printf("%s\n",t? "Yes.":"No.");
if(t==1)
{
a=1;
b=1;
while(a<=n)
{
if(o2[a]==o1[b])
{
printf("in\n");
a++;
b++;
printf("out\n");
}
else if(!c.empty()&&c.top()==o2[a])
{
c.pop();
a++;
printf("out\n");
}
else if(b<=n)
{
printf("in\n");
c.push(o1[b++]);
}
}
}
printf("FINISH\n");
}
return 0;
}
问题描述
As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes a problem, there is only one railway where all the trains stop. So all the trains come in from one side and get out from the other side. For this problem, if train A gets into the railway first, and then train B gets into the railway before train A leaves, train A can't leave until train B leaves. The pictures below figure out the problem. Now the problem for you is, there are at most 9 trains in the station, all the trains has an ID(numbered from 1 to n), the trains get into the railway in an order O1, your task is to determine whether the trains can get out in an order O2.
Input
The input contains several test cases. Each test case consists of an integer, the number of trains, and two strings, the order of the trains come in:O1, and the order of the trains leave:O2. The input is terminated by the end of file. More details in the Sample Input.
Output
The output contains a string "No." if you can't exchange O2 to O1, or you should output a line contains "Yes.", and then output your way in exchanging the order(you should output "in" for a train getting into the railway, and "out" for a train getting out of the railway). Print a line contains "FINISH" after each test case. More details in the Sample Output.
Sample Input
3 123 321
3 123 312
Sample Output
Yes.
in
in
in
out
out
out
FINISH
No.
FINISH
杭电1022 很简单的栈问题 在编译器上正确输出 为什么提交却是 WA? 坐等解惑~~
答案:1 悬赏:10
解决时间 2021-02-18 12:22
- 提问者网友:情系雨樱花
- 2021-02-17 13:26
最佳答案
- 二级知识专家网友:一身浪痞味
- 2021-02-17 14:09
#include<cstdio>
#include<stack>
#include<cstring>
using namespace std;
int main()
{
int i,a,b,t,n;
char o1[10000],o2[10000];
char input;
stack<char> c;
while(scanf("%d",&n)!=EOF)
{
while(!c.empty()) c.pop(); // 需要清空c
while((input = getchar())==' ');
ungetc(input,stdin);
for(i=1;i<=n;i++)
scanf("%c",&o1[i]);
while((input = getchar())==' ');
ungetc(input,stdin);
for(i=1;i<=n;i++)
scanf("%c",&o2[i]);
a=1;
b=1;
t=1;
while(a<=n)
{
if(!c.empty()&&c.top()==o2[a])
{
c.pop();
a++;
}
else if(b<=n)
{
c.push(o1[b++]);
}
else
{
t=0;
break;
}
}
printf("%s\n",t? "Yes.":"No.");
if(t==1)
{
a=1;
b=1;
while(a<=n)
{
if(!c.empty()&&c.top()==o2[a])
{
c.pop();
a++;
printf("out\n");
}
else if(b<=n)
{
printf("in\n");
c.push(o1[b++]);
}
}
}
printf("FINISH\n");
}
return 0;
}
#include<stack>
#include<cstring>
using namespace std;
int main()
{
int i,a,b,t,n;
char o1[10000],o2[10000];
char input;
stack<char> c;
while(scanf("%d",&n)!=EOF)
{
while(!c.empty()) c.pop(); // 需要清空c
while((input = getchar())==' ');
ungetc(input,stdin);
for(i=1;i<=n;i++)
scanf("%c",&o1[i]);
while((input = getchar())==' ');
ungetc(input,stdin);
for(i=1;i<=n;i++)
scanf("%c",&o2[i]);
a=1;
b=1;
t=1;
while(a<=n)
{
if(!c.empty()&&c.top()==o2[a])
{
c.pop();
a++;
}
else if(b<=n)
{
c.push(o1[b++]);
}
else
{
t=0;
break;
}
}
printf("%s\n",t? "Yes.":"No.");
if(t==1)
{
a=1;
b=1;
while(a<=n)
{
if(!c.empty()&&c.top()==o2[a])
{
c.pop();
a++;
printf("out\n");
}
else if(b<=n)
{
printf("in\n");
c.push(o1[b++]);
}
}
}
printf("FINISH\n");
}
return 0;
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯
• 手机登qq时,显示手机磁盘不足,清理后重新登 |
• 刺客的套装怎么选啊? |