QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#239338#7686. The Phantom Menaceucup-team2303#TL 0ms0kbC++174.5kb2023-11-04 20:10:252023-11-04 20:10:26

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)
  • [2023-11-04 20:10:26]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2023-11-04 20:10:25]
  • 提交

answer

// #pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#include <bits/stdc++.h>
using namespace std;

#define PB emplace_back
// #define int long long
#define ll long long
#define vi vector<int>
#define siz(a) ((int) ((a).size()))
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
void print(vi n) {rep(i, 0, siz(n) - 1) cerr << n[i] << " \n"[i == siz(n) - 1];}

const int N = 2e6;
int a, b, id, ch[N + 5][26], fail[N + 5], p[N + 5], q[N + 5], ans[N + 5], dep[N + 5];
string s[N + 5], t[N + 5];
struct pii {
	int x, y;
};
vi ss[N + 5], tt[N + 5];
vector<pii> f[N + 5], g[N + 5], st[N + 5];

struct node {
	int rt;
	void ins(int n, string &s, int o) {
		if(!rt) rt = ++id;
		int now = rt;
		if(!o) ss[n].clear(), ss[n].PB(rt);
		else tt[n].clear(), tt[n].PB(rt);
		rep(i, 0, b - 1) {
			// if(s[i] < 'a') cerr << s[i] << endl;
			int &v = ch[now][s[i] - 'a'];
			if(!v) v = ++id, dep[v] = dep[now] + 1;
			now = v;
			if(!o) ss[n].PB(now);
			else tt[n].PB(now);
		}
	}
	void build() {
		queue<int> qu;
		rep(i, 0, 25) {
			if(ch[rt][i]) qu.push(ch[rt][i]), fail[ch[rt][i]] = rt;
			else ch[rt][i] = rt;
		}
		while(siz(qu)) {
			int u = qu.front();
			qu.pop();
			rep(i, 0, 25) {
				int &v = ch[u][i];
				if(v) {
					fail[v] = ch[fail[u]][i];
					qu.push(v);
				}
				else {
					v = ch[fail[u]][i];
					if(!v) v = rt;
				}
			}
		}
	}
	int find(string &s) {
		int now = rt;
		rep(i, 0, siz(s) - 1) now = ch[now][s[i] - 'a'];
		return now;
	}
} S, T;

vi tmp[N + 5];
bool ck() {
	rep(i, 1, id) tmp[i].clear();
	rep(i, 1, a) p[i] = S.find(s[i]), q[i] = S.find(t[i]);
	rep(i, 1, a) tmp[p[i]].PB(i);
	rep(i, 1, a) {
		if(!siz(tmp[q[i]])) return 0;
		else ans[tmp[q[i]].back()] = i, tmp[q[i]].pop_back();
	}
	rep(i, 1, a) cout << i << " \n"[i == a];
	rep(i, 1, a) cout << ans[i] << " \n"[i == a];
	return 1;
}

vi qu, s1, s2;
int in[N + 5], out[N + 5];
void dfs(int n) {
	for(pii v; siz(st[n]); ) {
		v = st[n].back();
		st[n].pop_back();
		dfs(v.x);
		if(v.y > a) s2.PB(v.y - a);
		else s1.PB(v.y);
	}
}
bool fk() {
	s1.clear(), s2.clear();
	for(int x : qu) if(in[x] != out[x]) return 0;
	bool res = 1;
	for(int x : qu) {
		if(siz(st[x])) {
			if(res) dfs(x), res = 0;
			else return 0;
		}
	}
	return 1;
}

signed main() {
	// freopen(".in", "r", stdin);
	// freopen(".out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	int TT;
	for(cin >> TT; TT--; ) {
		rep(i, 1, id) memset(ch[i], 0, sizeof(ch[i])), fail[i] = 0;
		id = 0, S.rt = T.rt = 0;
		cin >> a >> b;
		rep(i, 1, a) cin >> s[i], S.ins(i, s[i], 0);
		rep(i, 1, a) cin >> t[i], T.ins(i, t[i], 1);
		S.build(), T.build();
		if(ck()) continue;
		rep(i, 1, a) {
			int now = T.find(s[i]);
			f[i].clear();
			for(; now != T.rt; f[i].PB(pii {now, dep[now]}), now = fail[now]);
		}
		rep(i, 1, a) {
			int now = S.find(t[i]);
			g[i].clear();
			for(; now != S.rt; g[i].PB(pii {now, dep[now]}), now = fail[now]);
			reverse(g[i].begin(), g[i].end());
		}
		bool ok = 0;
		rep(i, 1, b - 1) {
			bool is = 1;
			rep(j, 1, a) while(siz(f[j]) && f[j].back().y < i) f[j].pop_back();
			rep(j, 1, a) while(siz(g[j]) && g[j].back().y > b - i) g[j].pop_back();
			rep(j, 1, a) is &= siz(f[j]) && f[j].back().y == i;
			rep(j, 1, a) is &= siz(g[j]) && g[j].back().y == b - i;
			// cerr << i << endl;
			if(!is) continue;
			// cout << f[1].back().y << endl;
			rep(j, 1, a) {
				qu.PB(ss[j][b - i]);
				++out[ss[j][b - i]];
				qu.PB(f[j].back().x);
				++in[f[j].back().x];
				st[ss[j][b - i]].PB(pii {f[j].back().x, j});
			}
			rep(j, 1, a) {
				qu.PB(tt[j][i]);
				++out[tt[j][i]];
				qu.PB(g[j].back().x);
				++in[g[j].back().x];
				st[tt[j][i]].PB(pii {g[j].back().x, j + a});
			}
			// for(int x : qu) cout << x << endl;
			if(fk()) {
				assert(siz(s1) == a && siz(s2) == a);
				reverse(s1.begin(), s1.end());
				reverse(s2.begin(), s2.end());
				rep(i, 0, a - 1) cout << s1[i] << " \n"[i == a - 1];
				rep(i, 0, a - 1) cout << s2[i] << " \n"[i == a - 1];
				ok = 1;
				for(int x : qu) st[x].clear(), in[x] = out[x] = 0;
				qu.clear();
				break;
			}
			for(int x : qu) st[x].clear(), in[x] = out[x] = 0;
			qu.clear();
		}
		if(!ok) {
			cout << "-1\n";
			while(1);
		}
	}
	return cerr << endl << 1.0 * clock() / CLOCKS_PER_SEC << endl, 0;
}

详细

Test #1:

score: 0
Time Limit Exceeded

input:

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

output:


result: