QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#646533#7686. The Phantom MenaceFUCKUCUPTL 23ms134756kbC++206.6kb2024-10-17 00:23:482024-10-17 00:23:49

Judging History

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

  • [2024-10-17 00:23:49]
  • 评测
  • 测评结果:TL
  • 用时:23ms
  • 内存:134756kb
  • [2024-10-17 00:23:48]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000010, maxm = 4e6 + 10;
int T, n, m, len, s[maxm];
int sa[maxm], rk[maxm], t[maxm], x[maxm], y[maxm], ht[maxm], st[maxm][22], lg[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;
  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) {
    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)), 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[i][0] = 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[i][j + 1] = min(st[i][j], st[i - (1 << j)][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[x + (1 << l)][l], st[y][l]);
}
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];
  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 <= 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;
  while(T--) solve();
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 23ms
memory: 134756kb

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: -100
Time Limit Exceeded

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: