中易网

杭电1022 很简单的栈问题 在编译器上正确输出 为什么提交却是 WA? 坐等解惑~~

答案:1  悬赏:10  
解决时间 2021-02-18 12:22
#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
最佳答案
#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;
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
房产证是父母的名字母亲去世需要改成父亲的名
志华红木怎么去啊,有知道地址的么
您好。请问水沸腾前,气泡(是空气泡)上升时
植树节在大学里有什么节目,活动比较适合,
暄奕布行在什么地方啊,我要过去处理事情
北京圣思园张龙的java培训视频效果怎么样啊?
缝纫机电脑马达怎样调速
鲜花盛开尚且多日,人之坚持难逾三时什么意思
林州市几何房地产开发有限公司地址在哪,我要
移动可以设置外地号码亲情号码吗
23人准备乘坐缆车,每辆限乘4人,一共需要几辆
为什么手机上十分钟的网就百分之二十的电没了
农业银行是不是全国通用我想在江苏办理农行卡
没有高压锅炖排骨炖土豆做法大全
写字楼里,午餐,白领们最喜欢吃什么?
推荐资讯
赛欧跑了一万公里才换机油会有问题吗
马术比赛中一匹马价格的参考数据?
不同温度时苯乙烯的温度表,比热容表,黏度表
老人家晚上起来不方便,老人机有没有带强光手
请告诉我一些有关毕业话题的外文歌,谢谢!越
我是中专学历的男士,今年21岁,请问我要是在
谁知道爱情数学公式是什么?
怎样才能放下,怎样才能忘记一个不爱我的人?
跪求山东省威海市以下旅游景点现在票价:1.刘
工业区什么零食好卖,我想开家零食店,这里消
三到六年级的情态动词和副词
我是女生,过了年我就要满15岁了,我159cm算
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?