深搜,最后一个WA,求调
查看原帖
深搜,最后一个WA,求调
582049
_rainbow_楼主2022/11/12 18:18
#include<bits/stdc++.h>
#include<map>
using namespace std;
string A,B,a[100],b[100];
int n=1,ans=2000000000;
void dfs(string x,int step){
	if(step>10){
		return;
	} 
	if(x==B){
		if(step<ans){
			ans=step;
		}
		return;
	}
	for(int i=1;i<=n;i++){
		int pos=x.find(a[i]);
		if(pos>-1){
			string newstr=x;
			newstr.replace(pos,a[i].size(),b[i]);
			dfs(newstr,step+1);
		}
	}
}
int main(){
	cin>>A>>B;
	while(cin>>a[n]>>b[n]){
		n++;
	}
	n--;
	dfs(A,0);
	if(ans<=10){
		cout<<ans<<endl;
	}
	else{
		cout<<"NO ANSWER!"<<endl;
	}
	return 0;
} 
2022/11/12 18:18
加载中...