求助,5#wa,输出了NO ANSWER!,迭代加深

P1032 [NOIP2002 提高组] 字串变换

加<>的地方是改了的 ```cpp #include<bits/stdc++.h> using namespace std; string po[7],lp[7],be,en;//po[]为可被替换成的string,lp[]为替换成的string int P=1,ans; bool flag=0; /*<<<<<*/map <string,int> m;/*>>>>>*/ void dfs(string s,int step){ /*<<<<<*/m[s] = step;/*>>>>>*/ if(step==ans){ if(s==en) flag=1; return; } /*<<<<<*/int x;/*>>>>>*/ for(int i=1;i<=P;i++){ /*<<<<<*/ x = s.find(po[i]); while(x!=-1){ string g=s; g.replace(x,po[i].size(),lp[i]); //g还没出现 或 步数小于之前的最小步数 if (!m.count(g) || step + 1 < m[g]){ dfs(g,step+1); } x = s.find(po[i],x + 1); } /*>>>>>*/ } } int main(){ cin>>be>>en; while(cin>>po[P]>>lp[P]) P++;P--; for(ans=1;ans<=10;ans++){ /*<<<<<*/m.clear();/*>>>>>*/ dfs(be,0); if(flag==1) break; if(ans==10){ cout<<"NO ANSWER!"; return 0; } } cout<<ans; return 0; } ``` @[wzymzyc](/user/579857)
by 某个新手 @ 2022-07-26 22:09:25


谢谢
by popossible @ 2022-08-02 19:35:01


|