终于AC了,题目犯了2个很低级的错误,调试了蛮久~
一个是写的着急了,队列读取后忘记了出栈。还有一个是赋值操作符==竟然写成了=,自己根本没有留意,太可怕了~其实自己用心点,把左右值互换的话,编译的·时候就会报错的,习惯没有养好。
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
int main(int argc, char* argv[])
{
int n,i,j,k,flag,cou;
char o1[100],o2[100]; //之前用的int不对
while(cin>>n>>o1>>o2) //处理每一次出入车
{
i=0;
j=0;
k=0;
flag=0; //是否成功的标志
cou=0;
stack<int> p;
queue<int> q;
while(j<n) //n辆车出站
{
//printf("%d %d ",i,j);
//printf("%d %d",o1[i],o2[j]);
if(i<=n-1&&o2[j]==o1[i])
{
i++;
j++;
q.push(0); //顺序
q.push(1);
//printf("%d",p.top());
//printf("%d",o2[j]);
}
else
{
if(!p.empty()) //栈出车
{
if(p.top()==o2[j])
{
p.pop();
j++;
q.push(1);
}
else if(p.top()!=o2[j]&&i==n) //失败
{
cout<<"No."<<endl;
flag=1;
break;
}
else //这个漏掉了
{
p.push(o1[i]);
i++;
q.push(0);
//printf("%d\n",q.front());
}
}
else //入车进栈
{
p.push(o1[i]);
i++;
q.push(0);
//printf("%d\n",q.front());
}
}
}
if(flag==0) //输出,竟然将判断符号写成了赋值符号
{
cout<<"Yes."<<endl;
cou=q.size();
while(k<cou)
{
if(!q.front())
{
q.pop();
k++;
cout<<"in"<<endl;
}
else
{
q.pop();
k++;
cout<<"out"<<endl;
}
}
}
cout<<"FINISH"<<endl;
}
return 0;
}
看了别人的代码,自己的思路貌似麻烦了一些~ 输出时不用队列,用数组就蛮省事的了
d[c++]=1;
d[c++]=0;