萌新求助,为什么RE了,好像是字符串处理出错了,但我找不到``````
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
using namespace std;
string ST,AIM,gz[10],bh[10];
int q=0;
map<string,bool> vs;
int bfs(string s)
{
queue<string> Q1;
queue<int> CS;
Q1.push(s);
CS.push(0);
vs[s]=1;
while(Q1.size()&&CS.front()<=10&&Q1.front()!=AIM)
{
string nw=Q1.front();
int nwt=CS.front();
Q1.pop();CS.pop();
vs[nw]=1;
if(nw==AIM){
if(nwt<=10){
return nwt;
}
else{
return -1;
}
}
for(int i=1;i<=q;i++){
if(int(nw.find(gz[i]))==-1||gz[i].empty()==1)continue;
for(int j=0;j<nw.size();i++){
int ct=nw.find(gz[i],j);
if(ct!=-1){
string k=nw;//k为nw的副本
k.replace(ct,gz[i].size(),bh[i]);
ct=gz[i].size()+ct-1;
if(vs[k]==0&&gz[i].empty()==0){
vs[k]=1;
if(k==AIM){
if(nwt+1<=10){
return nwt+1;
}
else return -1;
}
else{
Q1.push(k);
CS.push(nwt+1);
}
}
}
}
}
cout<<Q1.empty()<<endl;
}
if(CS.front()<=10&&Q1.front()==AIM){
return CS.front();
}
else return -1;
}
int main(){
string x;
cin>>ST>>AIM;
getchar();
while(getline(cin,x)){
int ct=x.find(' ');
q++;
gz[q]=x.substr(0,ct);
bh[q]=x.substr(ct+1,x.size()-ct);
}
int w=bfs(ST);
if(w==-1){
cout<<"NO ANSWER!";
return 0;
}
cout<<w;
return 0;
}