最后一点TLE
查看原帖
最后一点TLE
398155
某kob在此楼主2021/3/8 21:14

我也不知道该说什么好了。。。。

希望大佬给个优化方案

orz

# include <bits/stdc++.h>
using namespace std;

struct str
{
	string sss;
	int step;
}x;

int len, times;
queue<str> q;
string s1, s2, a, b, s, y;
map<string, bool> vis;
string m[1005], p[1005];

int main(void)
{
	cin >> s1 >> s2;
	
	len = 1;
	while(cin >> m[len] >> p[len])
		len ++;

	int j;
	q.push({s1, 0});
	while(!q.empty() && x.step < 11)
	{
		x = q.front();
		q.pop();
		
		for (int i = 0; i <= x.sss.size() - 1; i ++)
		{
			y = x.sss[i];
			
			for (j = i + 1; j <= x.sss.size() - 1; j ++)
			{
				for (int l = 1; l <= len; l ++)
				{
					if (m[l] == y)
					{
						s = x.sss;
						s.erase(i, j - i);
						s.insert(i, p[l]);
						if (s == s2)
						{
							cout << x.step + 1 << endl;
							return 0;
						}
						if (!vis[y])	q.push({s, x.step + 1});
						vis[s] = 1;
					}
				}
				y += x.sss[j];
			}

			for (int l = 1; l <= len; l ++)
			{
				if (m[l] == y)
				{
					s = x.sss;
					s.erase(i, j - i);
					s.insert(i, p[l]);
					if (s == s2)
					{
						cout << x.step + 1 << endl;
						return 0;
					}
					if (!vis[y])	q.push({s, x.step + 1});
					vis[s] = 1;
				}
			}
		}
	}
	cout << "NO ANSWER!" << endl;
	
	return 0;
}
2021/3/8 21:14
加载中...