QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#625492#7686. The Phantom Menacelmx111TL 299ms5628kbC++203.3kb2024-10-09 19:31:342024-10-09 19:31:35

Judging History

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

  • [2024-10-09 19:31:35]
  • 评测
  • 测评结果:TL
  • 用时:299ms
  • 内存:5628kb
  • [2024-10-09 19:31:34]
  • 提交

answer

#include <bits/stdc++.h>
#define int long long 
#define endl '\n'
using namespace std;
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;
	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;
	}
	map<string, int>z1;
	map<string, int>z2;
	int tot1 = 0, tot2 = 0;
	for (int i = 1; i < m; i++) {
		for (int i = 1; i <= min(100005ll, (tot1 + tot2) * 4); i++) {
			z[i].clear();
		}
		z1.clear();
		z2.clear();
		tot1 = 0, tot2 = 0;
		for (int j = 1; j <= n; j++) {
			string pre = s1[j].substr(1, i);
			string las = s1[j].substr(i + 1, m - i);
			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++) {
			string pre = s2[j].substr(1, m - i);
			string las = s2[j].substr(m - i + 1, i);
			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(n * 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();
			}
			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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 299ms
memory: 5628kb

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 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
...

output:

1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
1 
1 
1 
1 
-1
-1
...

result:

ok 1000000 cases (1000000 test cases)

Test #3:

score: -100
Time Limit Exceeded

input:

500000
1 2
dd
ba
1 2
cd
ba
1 2
bd
ba
1 2
ad
ba
1 2
dc
ba
1 2
cc
ba
1 2
bc
ba
1 2
ac
ba
1 2
db
ba
1 2
cb
ba
1 2
bb
ba
1 2
ab
ba
1 2
da
ba
1 2
ca
ba
1 2
ba
ba
1 2
aa
ba
1 2
dd
aa
1 2
cd
aa
1 2
bd
aa
1 2
ad
aa
1 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