悬棺
查看原帖
悬棺
745067
封禁用户楼主2023/10/4 18:08

广搜,#3和#5TLE,求调

#include<bits/stdc++.h>
using namespace std;
struct node{
	string s;
	int step;
};
string a,b,x,y,da[10010],db[10010];
int tot;
void bfs(){
	queue<node>q;
	node tmp;tmp.s=a;tmp.step=0;
	q.push(tmp);
	map<string,bool>m;
	m[a]=1;
	while(!q.empty()){
		tmp=q.front();q.pop();
		if(tmp.s==b){
			cout<<tmp.step;
			exit(0);
		}
		for(int i=1;i<=tot;i++){
			for(int j=0;j<tmp.s.size();j++){
				bool flag=true;
				for(int k=0;k<da[i].size();k++)
					if(tmp.s[j+k]!=da[i][k]){
						flag=false;
						break;
					}
				if(flag){
					node u=tmp;
					u.s.replace(j,da[i].length(),db[i]);u.step++;
					q.push(u);
				}
			}
		}
	}
	cout<<"NO ANSWER!";
}
int main(){
	cin>>a>>b;
	while(cin>>x>>y){
		tot++;da[tot]=x;db[tot]=y;
	}
	bfs();
	return 0;
}

2023/10/4 18:08
加载中...