#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define f(III,MMM,NNN) for(int III=MMM;III<=NNN;III++)
typedef long long int ll;
const int N=1010;
string st,c;
string ch[110][3];
int n;
int ans=INT_MAX;
map<string,int> ma;
void dfs(string s,int sum){
// cout<<s<<endl;
// cout<<sum<<' ';
if(sum==11){
return ;
}
if(s==c){
ans=min(sum,ans);
return ;
}
// cout<<s<<' '<<c<<endl;
for(int i=1;i<=n;i++){
int pos=0;
string ss=s;
int nn=sum;
while(ss.find(ch[i][1],pos)!=-1){
ss.replace(ss.find(ch[i][1],pos),ch[i][1].length(),ch[i][2]);
pos=ss.find(ch[i][1],pos)+ch[i][1].length();
nn++;
if(ma.count(ss)!=1)
ma[ss]=1,
dfs(ss,nn);
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>st>>c;
n=1;
while(cin>>ch[n][1]>>ch[n][2]) n++;
n--;
dfs(st,0);
if(ans==INT_MAX)
cout<<"NO ANSWER!";
else
cout<<ans;
return 0;
}
我的程序输出NO ANSWER!
abaaaba abcdaba
a b
b d
d e
e f
f g
g c