谁能救救我,MLE了
查看原帖
谁能救救我,MLE了
1382496
myl_coder楼主2024/12/1 11:41
#include <bits/stdc++.h>
using namespace std;
//MLE~ 未完成
string a, b;
string A[10], B[10];
int cnt = 0;
struct node {
	string s;
	int t;
};
queue <node> q;
string s;
string fun (string tt, int j, string A, string B) {
	s = "";
	for (int i = 0; i < j; i ++) s.push_back(tt[i]);
	for (int i = 0; i < B.size(); i ++) s.push_back(B[i]);
	for (int i = j + A.size(); i < tt.size(); i ++) s.push_back (tt[i]);
	return s;
}

void bfs () {
	q.push ((node){a, 0});
//	cout << q.front().s << endl;
	while (q.size()) {

// 		cout << q.front().s << endl;
//		if (q.front().s.size() > 10) exit(0);
        if (q.front().t > 10) break ;
		if (q.front().s == b) {
			cout << q.front().t << endl;
			return ;
			
		}
		for (int i = 1; i <= cnt; i ++) {
			int j = 0;
			string tt = q.front().s;
			while (1) {
				j = tt.find (A[i], j);
//				cout << j << endl;
				if (j > q.front().s.size()) break;
				
			
				q.push ((node){fun (tt, j, A[i], B[i]), q.front().t + 1});
				j ++;
			}
		}
		q.pop();
	}
	cout << "NO ANSWER!\n";
}

int main() {
	ios :: sync_with_stdio();
	cin.tie(0);
	cout.tie(0);
	
	//freopen (".in", "r", stdin);
	//freopen (".out", "w", stdout);
	
	cin >> a >> b;
	string s1, s2;
	
	while (cin >> s1) {
		cin >> s2;
		A[++ cnt] = s1;
		B[cnt] = s2;
		
	}
	bfs ();
	
	
	return 0;
}
2024/12/1 11:41
加载中...