QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#646555 | #7686. The Phantom Menace | FUCKUCUP | WA | 889ms | 3932kb | C++20 | 6.9kb | 2024-10-17 00:48:38 | 2024-10-17 00:48:39 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010, maxm = maxn * 4;
int T, n, m, len, s[maxm];
int sa[maxm], rk[maxm], t[maxm], x[maxm], y[maxm], ht[maxm], lg[maxm], st[22][maxm];
int posa[maxn], posb[maxn], pos[maxn * 2], fa[maxn * 2], id[maxn * 2];
int p[maxn], q[maxn], ansp[maxn], ansq[maxn];
string a[maxn], b[maxn];
struct edge{ int to, nxt; } e[maxm];
int head[maxm], k, deg[maxm], stk[maxm], tp;
inline void sa_init() {
int cnt = n * 2 + 26;
for(int i = 1; i <= cnt; ++i) t[i] = 0;
// memset(t, 0, sizeof(int) * (cnt + 10));
for(int i = 1; i <= len; ++i) ++t[x[i] = s[i] + 1];
for(int i = 1; i <= cnt; ++i) t[i] += t[i - 1];
for(int i = len; i; --i) sa[t[x[i]]--] = i;
for(int k = 1; k <= len; k <<= 1) {
for(int i = 1; i <= cnt; ++i) t[i] = 0;
// memset(t, 0, sizeof(int) * (cnt + 10));
for(int i = 1; i <= len; ++i) ++t[x[i]];
for(int i = 1; i <= cnt; ++i) t[i] += t[i - 1];
int cnt2 = 0;
for(int i = len - k + 1; i <= len; ++i) y[++cnt2] = i;
for(int i = 1; i <= len; ++i) if(sa[i] > k) y[++cnt2] = sa[i] - k;
for(int i = len; i; --i) sa[t[x[y[i]]]--] = y[i];
// memset(y, 0, sizeof(int) * (len + 10));
for(int i = 1; i <= len; ++i) y[i] = 0;
swap(x, y), x[sa[1]] = cnt2 = 1;
for(int i = 2; i <= len; ++i) x[sa[i]] = (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k]) ? cnt2 : ++cnt2;
if(cnt2 == len) break;
else cnt = cnt2;
}
cnt = 0;
for(int i = 1; i <= len; ++i) rk[sa[i]] = i;
for(int i = 1; i <= len; ++i) if(rk[i] > 1) {
if(cnt) --cnt;
int j = sa[rk[i] - 1];
while(i + cnt <= len && j + cnt <= len && s[i + cnt] == s[j + cnt]) ++cnt;
ht[rk[i]] = cnt;
}
for(int i = 2; i <= len; ++i) st[0][i] = ht[i], lg[i] = lg[i >> 1] + 1;
for(int j = 0; j < 21; ++j) for(int i = (1 << j + 1) | 1; i <= len; ++i) st[j + 1][i] = min(st[j][i], st[j][i - (1 << j)]);
}
inline int LCP(int x, int y) {
if(x == y) return n - x + 1;
x = rk[x], y = rk[y];
// cout << x << ' ' << y << '\n';
if(x > y) swap(x, y);
int l = lg[y - x];
return min(st[l][x + (1 << l)], st[l][y]);
}
int getfa(int x) {
if(fa[x] == x) return x;
return fa[x] = getfa(fa[x]);
}
inline void add(int u, int v) {
// cout << u << ' ' << v << '\n';
e[++k] = {v, head[u]}, head[u] = k;
++deg[u], --deg[v];
}
void dfs(int u) {
// cout << u << '\n';
for(int i = head[u]; i; i = head[u]) head[u] = e[i].nxt, dfs(e[i].to);
stk[++tp] = u;
}
inline void solve() {
cin >> n >> m, len = 0;
for(int i = 1; i <= n; ++i) cin >> a[i];
for(int i = 1; i <= n; ++i) cin >> b[i];
// n = m = 1, len = 0;
// for(int i = 1; i <= n; ++i) a[i] = b[i] = "b";
for(int i = 1; i <= n; ++i) {
posa[i] = len + 1;
for(int j = 0; j < m; ++j) s[++len] = a[i][j] - 'a';
s[++len] = 25 + i;
}
for(int i = 1; i <= n; ++i) {
posb[i] = len + 1;
for(int j = 0; j < m; ++j) s[++len] = b[i][j] - 'a';
s[++len] = 25 + n + i;
}
sa_init();
// cout << len << '\n';
// for(int i = 1; i <= len; ++i) cout << s[i] << ' '; cout << '\n';
// for(int i = 1; i <= len; ++i) cout << rk[i] << ' '; cout << '\n';
// for(int i = 1; i <= len; ++i) cout << ht[i] << ' '; cout << '\n';
for(int i = 1; i <= n; ++i) p[i] = q[i] = i;
sort(p + 1, p + 1 + n, [&](int x, int y) {
// cout << x << ' ' << y << ' ' << posa[x] << ' ' << posa[y] << '\n';
int lcp = LCP(posa[x], posa[y]);
// cout << x << ' ' << y << ' ' << lcp << '\n';
return lcp == m || a[x][lcp] <= a[y][lcp];
});
sort(q + 1, q + 1 + n, [&](int x, int y) {
int lcp = LCP(posb[x], posb[y]);
return lcp == m || b[x][lcp] <= b[y][lcp];
});
// for(int i = 1; i <= n; ++i) cout << p[i] << ' '; cout << '\n';
// for(int i = 1; i <= n; ++i) cout << q[i] << ' '; cout << '\n';
// for(int i = 1; i <= n; ++i) cout << LCP(posa[p[i]], posb[q[i]]) << ' '; cout << '\n';
bool flag = true, flag2 = false;
for(int i = 1; i <= n; ++i) if(LCP(posa[p[i]], posb[q[i]]) < m) flag = false;
if(flag) for(int i = 1; i <= n; ++i) ansp[i] = p[i], ansq[i] = q[i];
else {
for(int d = 1; d < m; ++d) {
memset(head, 0, sizeof(int) * (3 * n + 10)), k = 1;
memset(deg, 0, sizeof(int) * (3 * n + 10));
int tot = 0;
for(int i = 1; i <= 2 * n; ++i) fa[i] = i;
for(int i = 1; i <= n; ++i) pos[i] = rk[posa[i] + d], pos[i + n] = rk[posb[i]];
sort(pos + 1, pos + 1 + 2 * n);
for(int i = 2; i <= n * 2; ++i) if(LCP(sa[pos[i]], sa[pos[i - 1]]) >= m - d) fa[getfa(i)] = getfa(i - 1);
for(int i = 1; i <= n * 2; ++i) if(fa[i] == i) id[i] = ++tot;
// for(int i = 1; i <= n; ++i) cout << rk[posa[i] + d] << ' '; cout << '\n';
// for(int i = 1; i <= n; ++i) cout << rk[posb[i]] << ' '; cout << '\n';
// for(int i = 1; i <= n * 2; ++i) cout << (sa[pos[i]] + m) / (m + 1) << ' '; cout << '\n';
// for(int i = 1; i <= n * 2; ++i) cout << id[getfa(i)] << ' '; cout << '\n';
for(int i = 1; i <= n * 2; ++i) {
int num = (sa[pos[i]] + m) / (m + 1);
// cout << num << ' ' << id[getfa(i)] << '\n';
if(num <= n) add(num, id[getfa(i)] + n * 2);
else add(id[getfa(i)] + n * 2, num);
// cout << "s\n";
}
for(int i = 1; i <= 2 * n; ++i) fa[i] = i;
for(int i = 1; i <= n; ++i) pos[i] = rk[posa[i]], pos[i + n] = rk[posb[i] + m - d];
sort(pos + 1, pos + 1 + 2 * n);
for(int i = 2; i <= n * 2; ++i) if(LCP(sa[pos[i]], sa[pos[i - 1]]) >= d) fa[getfa(i)] = getfa(i - 1);
for(int i = 1; i <= n * 2; ++i) if(fa[i] == i) id[i] = ++tot;
// for(int i = 1; i <= n; ++i) cout << posa[i] << ' '; cout << '\n';
// for(int i = 1; i <= n; ++i) cout << posb[i] + m - d << ' '; cout << '\n';
// for(int i = 1; i <= n * 2; ++i) cout << (sa[pos[i]] + m) / (m + 1) << ' '; cout << '\n';
// for(int i = 1; i <= n * 2; ++i) cout << id[getfa(i)] << ' '; cout << '\n';
for(int i = 1; i <= n * 2; ++i) {
int num = (sa[pos[i]] + m) / (m + 1);
if(num <= n) add(id[getfa(i)] + n * 2, num);
else add(num, id[getfa(i)] + n * 2);
}
// for(int i = 1; i <= n * 2 + tot; ++i) cout << deg[i] << ' '; cout << '\n';
bool flag3 = false;
for(int i = 1; i <= n * 2 + tot; ++i) if(deg[i]) flag3 = true;
if(flag3) continue;
tp = 0, flag2 = true;
for(int i = 1; i <= n; ++i) if(head[i]) dfs(i);
reverse(stk + 1, stk + 1 + tp);
// for(int i = 1; i <= tp; ++i) cout << stk[i] << ' '; cout << '\n';
for(int i = 1; i <= tp; i += 4) ansp[(i + 3) / 4] = stk[i], ansq[(i + 3) / 4] = stk[i + 2] - n;
break;
}
}
if(!flag && !flag2) return cout << "-1\n", void();
for(int i = 1; i <= n; ++i) cout << ansp[i] << ' '; cout << '\n';
for(int i = 1; i <= n; ++i) cout << ansq[i] << ' '; cout << '\n';
}
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
cin >> T;
// T = 1;
while(T--) solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3840kb
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: 889ms
memory: 3932kb
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: 0
Accepted
time: 494ms
memory: 3776kb
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 2 dc aa 1 2 cc aa 1 2 bc aa 1 2 ac aa 1 2 db aa 1 2 cb aa 1 2 bb aa 1 2 ab aa 1 2 da aa 1 2...
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 500000 cases (500000 test cases)
Test #4:
score: 0
Accepted
time: 496ms
memory: 3828kb
input:
500000 2 1 d d b a 2 1 c d b a 2 1 b d b a 2 1 a d b a 2 1 d c b a 2 1 c c b a 2 1 b c b a 2 1 a c b a 2 1 d b b a 2 1 c b b a 2 1 b b b a 2 1 a b b a 2 1 d a b a 2 1 c a b a 2 1 b a b a 2 1 a a b a 2 1 d d a a 2 1 c d a a 2 1 b d a a 2 1 a d a a 2 1 d c a a 2 1 c c a a 2 1 b c a a 2 1 a c a a 2 1 d...
output:
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 1 2 2 1 -1 -1 2 1 2 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 2 1 2 1 2 1 2 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 1 2 1 2 -1 -1 2 1 1 2 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 1 2 1 2 -1 -1 -1 -1 -1 2 1 1 2 -1 -1 -1 -1 -1 -1 -1 -1 -...
result:
ok 500000 cases (500000 test cases)
Test #5:
score: 0
Accepted
time: 383ms
memory: 3848kb
input:
333333 1 3 cbb bfa 1 3 bbb bfa 1 3 abb bfa 1 3 fab bfa 1 3 eab bfa 1 3 dab bfa 1 3 cab bfa 1 3 bab bfa 1 3 aab bfa 1 3 ffa bfa 1 3 efa bfa 1 3 dfa bfa 1 3 cfa bfa 1 3 bfa bfa 1 3 afa bfa 1 3 fea bfa 1 3 eea bfa 1 3 dea bfa 1 3 cea bfa 1 3 bea bfa 1 3 aea bfa 1 3 fda bfa 1 3 eda bfa 1 3 dda bfa 1 3 c...
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 333333 cases (333333 test cases)
Test #6:
score: 0
Accepted
time: 366ms
memory: 3820kb
input:
333333 3 1 c b b b f a 3 1 b b b b f a 3 1 a b b b f a 3 1 f a b b f a 3 1 e a b b f a 3 1 d a b b f a 3 1 c a b b f a 3 1 b a b b f a 3 1 a a b b f a 3 1 f f a b f a 3 1 e f a b f a 3 1 d f a b f a 3 1 c f a b f a 3 1 b f a b f a 3 1 a f a b f a 3 1 f e a b f a 3 1 e e a b f a 3 1 d e a b f a 3 1 c...
output:
-1 -1 -1 2 3 1 3 1 2 -1 -1 -1 -1 -1 -1 -1 -1 -1 3 1 2 3 1 2 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 3 2 1 3 1 2 -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 2 1 3 3 1 2 -1 -1 -1 -1 -...
result:
ok 333333 cases (333333 test cases)
Test #7:
score: 0
Accepted
time: 317ms
memory: 3844kb
input:
250000 1 4 hbca fhaa 1 4 gbca fhaa 1 4 fbca fhaa 1 4 ebca fhaa 1 4 dbca fhaa 1 4 cbca fhaa 1 4 bbca fhaa 1 4 abca fhaa 1 4 haca fhaa 1 4 gaca fhaa 1 4 faca fhaa 1 4 eaca fhaa 1 4 daca fhaa 1 4 caca fhaa 1 4 baca fhaa 1 4 aaca fhaa 1 4 hhba fhaa 1 4 ghba fhaa 1 4 fhba fhaa 1 4 ehba fhaa 1 4 dhba fhaa...
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 250000 cases (250000 test cases)
Test #8:
score: 0
Accepted
time: 306ms
memory: 3864kb
input:
250000 4 1 h b c a f h a a 4 1 g b c a f h a a 4 1 f b c a f h a a 4 1 e b c a f h a a 4 1 d b c a f h a a 4 1 c b c a f h a a 4 1 b b c a f h a a 4 1 a b c a f h a a 4 1 h a c a f h a a 4 1 g a c a f h a a 4 1 f a c a f h a a 4 1 e a c a f h a a 4 1 d a c a f h a a 4 1 c a c a f h a a 4 1 b a c a f...
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 4 3 1 2 4 3 1 2 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...
result:
ok 250000 cases (250000 test cases)
Test #9:
score: 0
Accepted
time: 431ms
memory: 3840kb
input:
200000 1 5 jjjjj baaaa 1 5 ijjjj baaaa 1 5 hjjjj baaaa 1 5 gjjjj baaaa 1 5 fjjjj baaaa 1 5 ejjjj baaaa 1 5 djjjj baaaa 1 5 cjjjj baaaa 1 5 bjjjj baaaa 1 5 ajjjj baaaa 1 5 jijjj baaaa 1 5 iijjj baaaa 1 5 hijjj baaaa 1 5 gijjj baaaa 1 5 fijjj baaaa 1 5 eijjj baaaa 1 5 dijjj baaaa 1 5 cijjj baaaa 1 5 b...
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 200000 cases (200000 test cases)
Test #10:
score: 0
Accepted
time: 263ms
memory: 3836kb
input:
200000 5 1 j j j j j b a a a a 5 1 i j j j j b a a a a 5 1 h j j j j b a a a a 5 1 g j j j j b a a a a 5 1 f j j j j b a a a a 5 1 e j j j j b a a a a 5 1 d j j j j b a a a a 5 1 c j j j j b a a a a 5 1 b j j j j b a a a a 5 1 a j j j j b a a a a 5 1 j i j j j b a a a a 5 1 i i j j j b a a a a 5 1 h...
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 200000 cases (200000 test cases)
Test #11:
score: -100
Wrong Answer
time: 305ms
memory: 3904kb
input:
250000 2 2 hb ca fh aa 2 2 gb ca fh aa 2 2 fb ca fh aa 2 2 eb ca fh aa 2 2 db ca fh aa 2 2 cb ca fh aa 2 2 bb ca fh aa 2 2 ab ca fh aa 2 2 ha ca fh aa 2 2 ga ca fh aa 2 2 fa ca fh aa 2 2 ea ca fh aa 2 2 da ca fh aa 2 2 ca ca fh aa 2 2 ba ca fh aa 2 2 aa ca fh aa 2 2 hh ba fh aa 2 2 gh ba fh aa 2 2 f...
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 2 1 2 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 2 2 ...
result:
wrong answer p is not permutation (test case 97)