80分wa求助
查看原帖
80分wa求助
326254
LonginusMonkey楼主2023/7/13 16:08
#include<bits/stdc++.h>
using namespace std;
string A[7], B[7], from, to;int ans, tot, limit; bool bl = 0;
map<string, int> mp;
void dfs(string str, int bu) {
//	cout << str << endl;
	if(bu > limit) {
		return;
	}
	if(bl) {
		return;
	}
	if(str == to) {
		bl=1;
		return;
	}
	if(mp[str]) {
		return;
	}
	mp[str] = 1;
	for(int i=1; i<=tot; ++i) {
		int index = str.find(A[i]);
		if(index!=-1) {
			string str2 = str.substr(0, index) + B[i] + str.substr(index+A[i].length(), str.length());
			dfs(str2, bu+1);
		}
	}
}
void iddfs() {
	for(int i=0; i<=10; ++i) {
		mp.clear();
		bl = 0;
		limit = i;
		dfs(from, 0);
		if(bl) {
			cout << i;
			return;
		}
	}
	cout << "NO ANSWER!";
}
int main() {
	cin >> from >> to;
	while(cin>>A[++tot]){
		cin >> B[tot];
	}
	tot--;
//	cout << from << " " << to << endl;
//	for(int i=1; i<=tot; ++i) {
//		cout << A[i] << " " << B[i] << endl;
//	}
	iddfs();
	return 0;
}
2023/7/13 16:08
加载中...