莫名输出无解?

P1032 [NOIP2002 提高组] 字串变换

魔改一波还是不对 搞不懂发生了什么 ```cpp #include<iostream> #include<string> #include<queue> #include<unordered_map> using namespace std; int n,ans; string A,B,a[10],b[10],fuck=" "; struct node { string str; int step; }; unordered_map<string,bool>book; queue<node>q; string replace_n(string str,string&a,string&b,int n)//将str中第n次出现的字串a替换成b { int pos=0; if(pos==-str.npos)return"NULL"; for(int i=1;i<=n;i++) { pos=str.find(a,pos+str.find(a)); if(pos==str.npos)return"NULL"; } return str.replace(pos,a.size(),b); } int main() { cin>>A>>B; A=" "+A;B=" "+B; while(++n,cin>>a[n]>>b[n]); q.push((node){A,0}); while(!q.empty()) { node now=q.front(),to; q.pop(); if(now.str==B)return cout<<now.step,0; for(auto i=1;i<n;i++) { for(auto j=1;j<10;j++) { if(now.step<10&&replace_n(now.str,a[i],b[i],j)!="NULL") { to=(node){replace_n(now.str,a[i],b[i],j),now.step+1}; if(!book[to.str]) book[to.str]=true,q.push(to); } } } } cout<<"NO ANSWER!"; // string aa="abcfffabadjs",bb="XxxX",cc="abc"; // if(aa.rfind(bb)!=aa.npos)cout<<aa.rfind(bb); // else cout<<"fuck\n"; } ```
by NewSjf @ 2019-08-24 16:28:48


![张家睿之遗照.png](https://i.loli.net/2019/09/27/I3G8gk6BKcNxU2s.png)
by Kiana_Kaslana @ 2019-09-27 22:22:23


|