QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#625544#7686. The Phantom Menacelmx111TL 372ms7672kbC++204.3kb2024-10-09 19:44:182024-10-09 19:44:19

Judging History

你现在查看的是最新测评结果

  • [2024-10-09 19:44:19]
  • 评测
  • 测评结果:TL
  • 用时:372ms
  • 内存:7672kb
  • [2024-10-09 19:44:18]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long 
#define endl '\n'
using namespace std;
using ull = unsigned long long;

using pii = pair<int, int>;

const int N = 4e6 + 1e3 + 7;

constexpr uint64_t P = (1ull << 61) - 1, bs = 1313131;

uint64_t mul(uint64_t a, uint64_t b) {
	uint64_t l1 = (uint32_t)a, h1 = a >> 32, l2 = (uint32_t)b, h2 = b >> 32;
	uint64_t l = l1 * l2, m = l1 * h2 + l2 * h1, h = h1 * h2;
	uint64_t ret = (l & P) + (l >> 61) + (h << 3) + (m >> 29) + (m << 35 >> 3) + 1;
	ret = (ret & P) + (ret >> 61);
	ret = (ret & P) + (ret >> 61);
	return ret - 1;
}

ull add(const ull& a, const ull& b) {
	return a + b >= P ? a + b - P : a + b;
}

ull pw[N];

ull geth(const vector<ull>& h, int l, int r)
{
	return add(h[r], P - mul(h[l - 1], pw[r - l + 1]));
}
const int maxn = 1000010;
int m, u, v, del[maxn];
int du[maxn][2];
stack<int>st;
vector<pair<int,int>>z[maxn];
void dfs(int now, int id) {
	for (int i = del[now]; i < z[now].size(); i = del[now]) {
		del[now] = i + 1;
		dfs(z[now][i].first, z[now][i].second);
	}
	st.push(id);
}
int ans[maxn];
bool check(int n) {
	for (int i = 1; i <= n; i++) {
		sort(z[i].begin(), z[i].end());
	}
	int S = 1, cnt[2] = { 0,0 };
	bool flag = 1;
	for (int i = 1; i <= n; i++) {
		if (du[i][1] != du[i][0]) {
			return 0;
		}
	}
	dfs(S, 1);
	int c = 0;
	while (!st.empty()) {
		ans[c++] = st.top();
		st.pop();
	}
	return 1;
}
void solve() {
	int n, m;
	cin >> n >> m;
	pw[0] = 1;
	for (int i = 1; i <= m; i++)
		pw[i] = mul(pw[i - 1], bs);
	vector<string>s1(n + 2);
	vector<string>s2(n + 2);
	for (int i = 1; i <= n; i++) {
		cin >> s1[i];
		s1[i] = '!' + s1[i];
	}
	for (int i = 1; i <= n; i++) {
		cin >> s2[i];
		s2[i] = '!' + s2[i];
	}
	map<string, int>zzz;
	vector<int>res;
	int flag = 0;
	for (int i = 1; i <= n; i++) {
		zzz[s1[i]] = i;
	}
	for (int i = 1; i <= n; i++) {
		if (zzz.count(s2[i])) {
			res.push_back(zzz[s2[i]]);
		}
		else {
			flag = 1;
			break;
		}
	}
	if (flag == 0) {
		for (int i = 0; i < n; i++) {
			cout << res[i] << ' ';
		}
		cout << endl;
		for (int i = 1; i <= n; i++) {
			cout << i << ' ';
		}
		cout << endl;
		return;
	}
	vector<vector<ull> >hs(n + 1, vector<ull>(m + 1)), ht(n + 1, vector<ull>(m + 1));
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			hs[i][j] = add(mul(hs[i][j - 1], bs), s1[i][j]), ht[i][j] = add(mul(ht[i][j - 1], bs), s2[i][j]);
	map<ull, int>z1;
	map<ull, int>z2;
	int tot1 = 0, tot2 = 0;
	for (int i = 1; i < m; i++) {
		for (int i = 1; i <= (tot1 + tot2); i++) {
			z[i].clear();
			du[i][0] = 0;
			du[i][1] = 0;
		}
		z1.clear();
		z2.clear();
		tot1 = 0, tot2 = 0;
		for (int j = 1; j <= n; j++) {
			ull pre = geth(hs[j], 1, i), las = geth(hs[j], i + 1, m);
			int a1, a2;
			if (z1.count(pre)) {
				a1 = z1[pre];
			}
			else {
				z1[pre] = ++tot1;
				a1 = tot1;
			}
			if (z2.count(las)) {
				a2 = z2[las];
			}
			else {
				z2[las] = ++tot2;
				a2 = tot2;
			}
			z[a1].push_back({ a2 + n * 2,j });
			du[a1][1]++;
			du[a2 + n * 2][0]++;
		}
		for (int j = 1; j <= n; j++) {
			ull pre = geth(ht[j], 1, m-i), las = geth(ht[j], m-i + 1, m);
			int a1, a2;
			if (z2.count(pre)) {
				a1 = z2[pre];
			}
			else {
				z2[pre] = ++tot1;
				a1 = tot1;
			}
			if (z1.count(las)) {
				a2 = z1[las];
			}
			else {
				z1[las] = ++tot2;
				a2 = tot2;
			}
			z[a1 + n * 2].push_back({ a2,j + n });
			du[a1 + n * 2][1]++;
			du[a2][0]++;
		}
		check(m * 2);
		if (ans[n * 2] != 0) {
			vector<int> p, q;
			for (int i = 1; i <= n * 2;i++) {
				int x = ans[i];
				if (x <= n) {
					p.push_back(x);
				}
				else {
					q.push_back(x - n);
				}
			}
			for (int i = 0; i < n; i++) {
				cout << p[i] << ' ';
			}
			cout << endl;
			for (int i = 0; i < n; i++) {
				cout << q[i] << ' ';
			}
			for (int i = 0; i <= n * 2 + 1; i++) {
				ans[i] = 0;
			}
			for (int i = 1; i <= min(100005ll, (tot1 + tot2) * 4); i++) {
				z[i].clear();
				du[i][0] = 0;
				du[i][1] = 0;
			}
			cout << endl;
			return;
		}
		for (int i = 0; i <= n*2+1; i++) {
			ans[i] = 0;
		}
	}
	cout << -1 << endl;
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int t;
	cin >> t;
	while (t--) {
		solve();
	}
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 7612kb

input:

2
3 3
abc
ghi
def
bcd
efg
hia
1 3
abc
def

output:

1 3 2 
1 2 3 
-1

result:

ok 2 cases (2 test cases)

Test #2:

score: 0
Accepted
time: 372ms
memory: 7672kb

input:

1000000
1 1
b
b
1 1
a
b
1 1
b
a
1 1
a
a
1 1
b
b
1 1
a
b
1 1
b
a
1 1
a
a
1 1
b
b
1 1
a
b
1 1
b
a
1 1
a
a
1 1
b
b
1 1
a
b
1 1
b
a
1 1
a
a
1 1
b
b
1 1
a
b
1 1
b
a
1 1
a
a
1 1
b
b
1 1
a
b
1 1
b
a
1 1
a
a
1 1
b
b
1 1
a
b
1 1
b
a
1 1
a
a
1 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