hdu1022 Train Problem I 总是不能AC

来源:百度知道 编辑:UC知道 时间:2024/05/26 12:23:49
题目在http://acm.hdu.edu.cn/showproblem.php?pid=1022
#include<iostream>
#include<string>
using namespace std;
int main(){
int N;
while(!cin.eof()){
N=0;
string str1,str2;
cin>>N>>str1>>str2;
int m=str1.length();
char t;
for(int i=0,j=m-1;i<j;i++,j--){
t=str1[i];
str1[i]=str1[j];
str1[j]=t;
}
if(str1==str2){
int count;
if(N!=0)
cout<<"Yes."<<endl;
for(count=0;count<N;count++)
cout<<"in"<<endl;
for(count=0;count<N;count++)
cout<<"out"<<endl;
if(N!=0)
cout<<"FINISH"<<endl;
}
else{
cout<<"No."<<endl;
cout<<"FINISH"<

这不是栈的应用吗

你就把字符串反一下是不对的, 火车的出入不一定按逆序输出的,

火车 123 的形式进来, 132 出来
123 in 1 out 1 in 2 in 3 out 3 out 2 出来是 132, 你的代码这样就不对了

#include<iostream>
using namespace std;

int main()
{
int n,i,j,k;
char in[1000],out[1000];
int flag[2000];

while(cin>>n>>in>>out)
{
char stack[1000];
int top=-1;
k=0,i=0,j=0;
while(j<n+1&&i<n)
if(stack[top]==out[i]&&top!=-1)
{
top--;
flag[k]=0;
k++;i++;
}
else
{
top++;
flag[k]=1;k++;
stack[top]=in[j];j++;
}
if(k!=2*n){cout<<"No."<<endl<<"FINISH"<<endl;continue;}
else cout<<