QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#307092#7686. The Phantom MenaceA_programmerTL 281ms226972kbC++174.9kb2024-01-17 22:11:252024-01-17 22:11:25

Judging History

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

  • [2024-10-08 14:11:03]
  • hack成功,自动添加数据
  • (/hack/941)
  • [2024-10-08 10:05:28]
  • hack成功,自动添加数据
  • (/hack/940)
  • [2024-10-07 19:51:15]
  • hack成功,自动添加数据
  • (/hack/938)
  • [2024-10-07 19:28:01]
  • hack成功,自动添加数据
  • (/hack/937)
  • [2024-10-07 17:16:32]
  • hack成功,自动添加数据
  • (/hack/936)
  • [2024-10-07 16:53:09]
  • hack成功,自动添加数据
  • (/hack/935)
  • [2024-10-07 16:22:17]
  • hack成功,自动添加数据
  • (/hack/934)
  • [2024-01-17 22:11:25]
  • 评测
  • 测评结果:TL
  • 用时:281ms
  • 内存:226972kb
  • [2024-01-17 22:11:25]
  • 提交

answer

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

typedef unsigned long long ull;
typedef pair<int, int> pii;
const int maxn = 1e6 + 5;
const ull base = 13131;

int mp[maxn << 2];
ull mi[maxn], C[maxn << 2];
int n, m, A[maxn][2], B[maxn][2], ida[maxn], idb[maxn];
vector<ull> prea[maxn], preb[maxn], sufa[maxn], sufb[maxn];
vector<pii> g[maxn << 1];
vector<int> ansA, ansB;
string a[maxn], b[maxn];
bool vis[maxn];
int cur[maxn];
stack<int> st;

bool cmpa(int x, int y) { return A[x][0] < A[y][0]; };
bool cmpb(int x, int y) { return B[x][0] < B[y][0]; }

void dfs(int u)
{
	vis[u] = 1;
	for (int i = cur[u]; i < g[u].size(); i++)
	{
		pii tmp = g[u][i];
		cur[u]++;
		dfs(tmp.first);
		st.push(tmp.second);
	}
}

bool work(int k)
{
	if (k == m)
	{
		int cnt = 0;
		for (int i = 1; i <= n; i++)
			if (!mp[prea[i][m - 1]]) mp[prea[i][m - 1]] = ++cnt;
		
		for (int i = 1; i <= n; i++) A[i][0] = mp[prea[i][m - 1]], B[i][0] = mp[preb[i][m - 1]];
		for (int i = 1; i <= n; i++) mp[prea[i][m - 1]] = 0;
		for (int i = 1; i <= n; i++) ida[i] = idb[i] = i;
		sort(ida + 1, ida + n + 1, cmpa);
		sort(idb + 1, idb + n + 1, cmpb);
		for (int i = 1; i <= n; i++) cout << ida[i] << " "; cout << "\n";
		for (int i = 1; i <= n; i++) cout << idb[i] << " "; cout << "\n";
		return true;
	}
	
	int cnt = 0;
	for (int i = 1; i <= n; i++)
		if (!mp[prea[i][k - 1]]) mp[prea[i][k - 1]] = ++cnt;
	for (int i = 1; i <= n; i++) A[i][0] = mp[prea[i][k - 1]], B[i][1] = mp[sufb[i][m - k]];
	for (int i = 1; i <= n; i++) mp[prea[i][k - 1]] = 0;
	for (int i = 1; i <= n; i++)
		if (!mp[sufa[i][k]]) mp[sufa[i][k]] = ++cnt;
	for (int i = 1; i <= n; i++) A[i][1] = mp[sufa[i][k]], B[i][0] = mp[preb[i][m - k - 1]];
	for (int i = 1; i <= n; i++) mp[sufa[i][k]] = 0;
	
	for (int i = 1; i <= cnt; i++) g[i].clear(), vis[i] = cur[i] = 0;
	for (int i = 1; i <= n; i++)
	{
		g[A[i][0]].emplace_back(make_pair(A[i][1], i));
		g[B[i][0]].emplace_back(make_pair(B[i][1], n + i));
	}
	while (st.size()) st.pop();
	dfs(1);
	if (st.size() != 2 * n) return false;
	while (st.size())
	{
		if (st.top() > n) ansB.emplace_back(st.top() - n);
		else ansA.emplace_back(st.top());
		st.pop();
	}
	for (int x : ansA) cout << x << " "; cout << "\n";
	for (int x : ansB) cout << x << " "; cout << "\n";
	return true;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	mi[0] = 1;
	for (int i = 1; i <= 1000000; i++) mi[i] = mi[i - 1] * base;
	
	int T;
	cin >> T;
	while (T--)
	{
		cin >> n >> m;
		for (int i = 1; i <= n; i++)
		{
			cin >> a[i];
			prea[i].resize(m), sufa[i].resize(m);
			for (int j = 0; j < m; j++) prea[i][j] = (j ? prea[i][j - 1] : 0ull) * base + a[i][j];
			for (int j = m - 1; ~j; j--)
				sufa[i][j] = (j == m - 1 ? 0ull : sufa[i][j + 1]) + mi[m - j - 1] * a[i][j];
		}
		
		for (int i = 1; i <= n; i++)
		{
			cin >> b[i];
			preb[i].resize(m), sufb[i].resize(m);
			for (int j = 0; j < m; j++) preb[i][j] = (j ? preb[i][j - 1] : 0ull) * base + b[i][j];
			for (int j = m - 1; ~j; j--)
				sufb[i][j] = (j == m - 1 ? 0ull : sufb[i][j + 1]) + mi[m - j - 1] * b[i][j];
		}
		
		int sz = 0;
		for (int i = 1; i <= n; i++)
		{
			for (int j = 0; j < m; j++) C[++sz] = prea[i][j], C[++sz] = preb[i][j];
			for (int j = 0; j < m; j++) C[++sz] = sufa[i][j], C[++sz] = sufb[i][j];
		}
		sort(C + 1, C + sz + 1);
		sz = unique(C + 1, C + sz + 1) - C - 1;
		
		for (int i = 1; i <= n; i++)
		{
			for (int j = 0; j < m; j++)
			{
				prea[i][j] = lower_bound(C + 1, C + sz + 1, prea[i][j]) - C;
				sufa[i][j] = lower_bound(C + 1, C + sz + 1, sufa[i][j]) - C;
				preb[i][j] = lower_bound(C + 1, C + sz + 1, preb[i][j]) - C;
				sufb[i][j] = lower_bound(C + 1, C + sz + 1, sufb[i][j]) - C;
			}
		}
		
		for (int i = 1; i <= n; i++) mp[prea[i][m - 1]]++;
		for (int i = 1; i <= n; i++) mp[preb[i][m - 1]]--;
		
		bool Fl = true;
		for (int i = 1; i <= n; i++)
			if (mp[prea[i][m - 1]])
			{
				Fl = false;
				break;
			}
		for (int i = 1; i <= n; i++) mp[prea[i][m - 1]] = 0;
		for (int i = 1; i <= n; i++) mp[preb[i][m - 1]] = 0;
		if (Fl)
		{
			work(m);
			continue;
		}
		
		bool Tl = false;
		for (int k = 1; k < m; k++)
		{
			for (int i = 1; i <= n; i++) mp[prea[i][k - 1]]++, mp[sufb[i][m - k]]--;
			Fl = true;
			for (int i = 1; i <= n; i++)
				if (mp[prea[i][k - 1]])
				{
					Fl = false;
					break;
				}
			for (int i = 1; i <= n; i++) mp[prea[i][k - 1]] = mp[sufb[i][m - k]] = 0;
			
			if (Fl)
			{
				for (int i = 1; i <= n; i++) mp[sufa[i][k]]++, mp[preb[i][m - k - 1]]--;
				Fl = true;
				for (int i = 1; i <= n; i++)
					if (mp[sufa[i][k]])
					{
						Fl = false;
						break;
					}
				for (int i = 1; i <= n; i++) mp[sufa[i][k]] = mp[preb[i][m - k - 1]] = 0;
			}
			
			if (Fl && work(k))
			{
				Tl = true;
				break;
			}
		}
		if (!Tl) cout << "-1\n";
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 20ms
memory: 224764kb

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: 281ms
memory: 226972kb

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