请求加强数据
  • 板块P1127 词链
  • 楼主Link_Cut_Y
  • 当前回复1
  • 已保存回复1
  • 发布时间2023/10/28 20:05
  • 上次更新2023/11/2 11:01:10
查看原帖
请求加强数据
519384
Link_Cut_Y楼主2023/10/28 20:05

很显然,暴力算法肯定跑不了。

但是这个暴力代码却过了。原因是加入了无脑卡时。

请求加强数据。

#include <algorithm>
#include <iostream>
#include <vector>
#include <ctime>

using namespace std;

const int N = 1010;
vector<int> E[N], path;
string str[N];
bool st[N];
int n;
double start;
void dfs(int u, int cnt) {
	if (cnt == n) {
		for (int i = 0; i < path.size() - 1; i ++ )
			cout << str[path[i]] << '.';
		cout << str[path.back()]; exit(0);
	}
	if ((double)clock() / CLOCKS_PER_SEC - start >= 0.005)
		return;
	for (auto i : E[u]) {
		if (st[i]) continue;
		path.push_back(i);
		st[i] = true; dfs(i, cnt + 1), st[i] = false;
		path.pop_back();
	}
}
int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i ++ )
		cin >> str[i];
	sort(str + 1, str + n + 1);
	for (int i = 1; i <= n; i ++ )
		for (int j = 1; j <= n; j ++ )
			if (i != j && str[i].back() == str[j].front())
				E[i].push_back(j);
	for (int i = 1; i <= n; i ++ )
		sort(E[i].begin(), E[i].end(), [&](int a, int b) {
			return str[a] < str[b];
		});
	for (int i = 1; i <= n; i ++ ) {
		st[i] = true; st[i - 1] = false;
		path.clear(); path.push_back(i);
		start = (double)clock() / CLOCKS_PER_SEC;
		dfs(i, 1);
	} puts("***");
	return 0;
}
2023/10/28 20:05
加载中...