第五个点WA了,不知道怎么改,大佬救救孩子吧

P1032 [NOIP2002 提高组] 字串变换

我也是第五个点WA,迷之错误,兄弟你bfs不用结构体看着有点难受 ```cpp #include<bits/stdc++.h> using namespace std; struct Change{ string a,b; }a[10]; int len=1; string s1,s2; struct NODE{ int step; string s; NODE(int a,string x) { step=a; s=x; } }; map<string,int> vis; int ans=-1; void bfs() { queue<NODE> Q; Q.push(NODE(0,s1)); while (!Q.empty()){ string t=Q.front().s; int step=Q.front().step; // cout<<t<<endl; Q.pop(); if (step>10) break; if (t==s2){ ans=step; return; } for (int i=1;i<=len;i++){ int pos=t.find(a[i].a); if (pos==t.npos) continue; string now; now=t.substr(0,pos)+a[i].b+t.substr(pos+a[i].a.size(),(t.size()-pos-a[i].a.size())); // cout<<now<<" "<<step+1<<endl; if (vis[now]) continue; vis[now]=1; Q.push(NODE(step+1,now)); } } return; } int main() { cin>>s1>>s2; while (cin>>a[len].a>>a[len].b) len++; len--; bfs(); if (ans==-1 || ans>10) cout<<"NO ANSWER!\n"; else cout<<ans<<endl; return 0; } ```
by 爱新觉罗·弘历 @ 2020-02-04 17:35:51


|