本喵今天累了,所以水一篇。
题目链接:link,请点击这里喵~
#include <stdio.h>
#include <ctype.h>
#include <algorithm>
#include <string>
#include <iostream>
#include <map>
#include <list>
#define lnt long long
#define inf 0x7fffffff
using namespace std;
int xx;char ff,chh;inline int read(){
xx=ff=0;while(!isdigit(chh)){if(chh=='-'){ff=1;}chh=getchar();}
while(isdigit(chh)){xx=(xx<<1)+(xx<<3)+chh-'0';chh=getchar();}return ff? -xx: xx;
}
const int N=50;
struct node{
string x;int cnt;
};
map<string,bool> ys;
string to[N][2];
int main(){
string a,b;
list<node> q;
cin>>a>>b;
int m=0;
while(cin>>to[m][0]>>to[m][1]){++m;}
q.push_back({a,0});
while(!q.empty()){
node u=q.front();
if(u.cnt>10){puts("NO ANSWER!");return 0;}
if(u.x==b){printf("%d",u.cnt);return 0;}
q.pop_front();string p=u.x;
for(int i=0;i<m;++i){
int e=0;
while((e=u.x.find(to[i][0],e))!=string::npos){
p=u.x;
p.replace(e,to[i][0].length(),to[i][1]);
++e;
if(ys.find(p)!=ys.end()){continue;}
ys[p]=1;q.push_back({p,u.cnt+1});
}
}
}
puts("NO ANSWER!");
return 0;
}