60分求调
查看原帖
60分求调
764499
Unreal414楼主2024/3/30 21:35

不知道是什么神奇的错误,希望各位大佬帮忙指出

/*@Unreal414 版权所有!!!*/
#include <bits/stdc++.h>
using namespace std;
string a, b;
map <string, int> step;
map <int, pair <string, string> > rules;
int solve() {
    if (a == b) return 0;
    queue <string> q;
    q.push(a);
    step[a] = 0;
    while (q.size()) {
        string &now = q.front();
        q.pop();
        if (step[now] == 10) return -1;
        for (auto &e : rules) {
        	int index = -1;
            while ((index = now.find(e.second.first, index + 1)) != string::npos) {
				string next = now;
            	next.replace(index, e.second.first.size(), e.second.second);
            	if (step.count(next)) continue;
	            step[next] = step[now] + 1;
	            if (next == b) return step[next];
	            q.push(next);
            }
        }
    }
    return -1;
}
int main() {
	int n = 0;
	cin >> a >> b;
	while (cin >> rules[n].first >> rules[n].second) n++;
	rules.erase(n);
	int result = solve();
	if (result == -1) {
		cout << "NO ANSWER!";
		return 0;
	}
	cout << result << "\n";
	return 0;
}
2024/3/30 21:35
加载中...