#4 MLE
查看原帖
#4 MLE
749831
Luogu_are_DASB楼主2023/11/3 18:53
#include <iostream>
#include <queue>
#include <map>
#include <string>
#include <cstring>
using namespace std;
const int N = 10;
string s ,s1;
struct Node {
	string a ,b;
}a[N];
struct node {
	int step;
	string f;
};
map<string ,bool> m;
queue<node> q;
//int n;
int pos[N];
int main() {
//	freopen("a.in","r",stdin);
//	freopen("a.out","w",stdout);
	cin >> s >> s1;
	int i1 = 1;
	for(int i = 1; i <= 6; i ++) {
		cin >> a[i].a >> a[i].b;
	}
	int n = 6;
	while(a[n].a[0] == '\0') n --;
	q.push((node){0 ,s});
	while(!q.empty()) {
		node t = q.front();
		if(t.f == s1) {
			cout << t.step;
			return 0;
		}
		if(t.step > 10) {
			cout << "NO ANSWER!";
			return 0;
		}
		if(m[t.f]) {
			q.pop();
			continue;
		}
		q.pop();
		m[t.f] = 1;
		memset(pos ,0 ,sizeof pos);
		for(int i = 1; i <= n; i ++) {
			if(t.f.find(a[i].a ,pos[i]) == string::npos) {
				
				continue;
			}
			else {
				while(t.f.find(a[i].a ,pos[i]) != string::npos) {
					string y = t.f;
					string s2 = t.f.replace(t.f.find(a[i].a ,pos[i]) ,a[i].a.size() ,a[i].b);
					pos[i] = t.f.find(a[i].a ,pos[i]);
					t.f = y;
					if(s2 == s1) {
						cout << t.step + 1;
						return 0;
					}
//				if(s2 == s1) {
//					cout << t
//				}
//				cout << t.step << '\n';
					q.push((node){t.step + 1 ,s2});
				}
				
//				break;
			}
		}
	}
//	if()
	cout << "NO ANSWER!";
	return 0;
} 
2023/11/3 18:53
加载中...