80分求助

P1032 [NOIP2002 提高组] 字串变换

用```map```写。 格式: ```cpp map<key,value> ``` 用map如下: ```cpp #include<bits/stdc++.h> using namespace std; const int K=1e3+10,N=1e4+10,M=1e8+10; string a[K],b[K],ss,s; int d,dv; queue<string> q; map<string,int> ans; map<string,bool> vis; int main() { cin>>a[0]>>b[0]; while(cin>>a[++d]>>b[d]); vis[a[0]]=true,q.push(a[0]); while(!q.empty()) { ss=s=q.front(); q.pop(); if(ans[s]<10) { if(s==b[0]) { printf("%d",ans[s]); return 0; } for(int i=1; i<=d; i++) { for(int j=0; j<s.size();j++) { s=ss; int found=s.find(a[i],j); if(found>-1) { s.replace(found,a[i].size(),b[i]); if(!vis[s]||!vis.count(s)) { q.push(s); vis[s]=true,ans[s]=ans[ss]+1; } } } } } } printf("NO ANSWER!"); return 0; } ```
by yyyyyyyyu @ 2023-11-14 20:10:13


|