~~奆,我到现在才20分~~
by JeffWang2019 @ 2020-06-21 11:14:58
@[JeffWang2019](/user/219935)
~~为什么我一发讨论就能看到你(How old are you?)~~
by chenxuanting @ 2020-06-21 13:26:11
@[chenxuanting](/user/220362) 因为我关注了你
by JeffWang2019 @ 2020-06-21 14:44:40
‮@[chenxuanting](/user/220362) 没错,就是这样
by JeffWang2019 @ 2020-06-21 14:44:59
@[JeffWang2019](/user/219935)
我谔谔
by chenxuanting @ 2020-06-21 14:58:38
你每次只找字串第一次出现的地方,当然出锅
这是改好的代码
```cpp
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
string st,ed;
string a[10],b[10];
map<string, bool> apr;//appear v.??
int num,ans=1000;
struct node
{
int step;
string cur;
};
void bfs()
{
queue<node> q;
q.push((node){0,st});
apr[st]=1;
while(!q.empty()){
node x=q.front();
q.pop();
if(x.step>10){
continue;
}
if(x.cur==ed){
ans=x.step;
break;
}
for(int i=1;i<=num;i++){
for(int j=0;j<=(signed)(x.cur.length()-a[i].length());++j)
if(x.cur.substr(j, a[i].size()) == a[i]){
string shadow=x.cur;
shadow.replace(j,a[i].size(),b[i]);
if(apr.count(shadow)==1){
continue;
}else{
q.push((node){x.step+1,shadow});
apr[shadow]=1;
}
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin>>st>>ed;
for(int i=1;i<=6;i++){
cin>>a[i]>>b[i];
if(a[i]==""){
break;
}
num++;
}
bfs();
if(ans==1000){
cout<<"NO ANSWER!";
}else{
cout<<ans;
}
return 0;
}
```
by namelessgugugu @ 2020-07-18 22:24:41