深搜,WA on #5,悬赏关注
查看原帖
深搜,WA on #5,悬赏关注
633466
LiaoYF1楼主2022/12/2 17:13
#include<iostream>
#include<string>
#include<map>
using namespace std;
map<string,string> m[15];
string a,b;
int x,t,ans=114514;
void dfs(string now,int k){
    if(k>10)return;
    if(now==b){
        ans=min(ans,k);
        return;
    }
    string s2;
    for(int j=0;j<=x;j++){
        for(auto i=m[j].begin();i!=m[j].end();i++){
            int pos=now.find(i->first);
            if(pos!=string::npos){
                s2=now;
                //cout<<k<<" "<<s2<<"\n";
                s2.replace(pos,(i->first).size(),i->second);
                //cout<<k<<" "<<now<<"\n";
                //cout<<k;
                dfs(s2,k+1);
                //\cout<<k<<" "<<now<<"\n";
            }
        }
    }
}
int main(){
    string ai,bi;
    cin>>a>>b;
    while(cin>>ai>>bi){
        if(m[t].count(ai)){
            x++;
            m[++t][ai]=bi;
            t--;
        }else{
            m[t][ai]=bi;
        }
    }
    // for(int k=0;k<=x;k++){
    //     for(auto i=m[k].begin();i!=m[k].end();i++){
    //         cout<<i->first<<" "<<i->second<<"\n";
    //     }
    //     cout<<"\n";
    // }
    //cout<<x;
    dfs(a,0);
    if(ans==114514){
        cout<<"NO ANSWER!";
    }else{
        cout<<ans;
    }
    return 0;
}
2022/12/2 17:13
加载中...