提交记录 27337


用户 题目 状态 得分 用时 内存 语言 代码长度
caijianhong 1006. 【模板题】后缀排序 Accepted 100 39.234 ms 9132 KB C++ 2.48 KB
提交时间 评测时间
2024-11-15 21:28:45 2024-11-15 21:28:51
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) fprintf(stderr, ##__VA_ARGS__)
#else
#define endl "\n"
#define debug(...) void(0)
#endif
using LL = long long;
struct machine {// {{{
  int n;
  vector<int> rk, sa, st[30], buc, id;
  machine() = default;
  explicit machine(const string& str)
      : n((int)str.size()), rk(n + 1), sa(n), buc(max(n, 128)), id(n) {
    rk[n] = -1;
    for (int i = 0; i < n; i++) buc[rk[i] = str[i]] += 1;
    for (int i = 1; i < 128; i++) buc[i] += buc[i - 1];
    for (int i = n; i--;) sa[--buc[rk[i]]] = i;
    int pre = id[sa[0]] = 0;
    for (int i = 1; i < n; i++) id[sa[i]] = (pre += rk[sa[i - 1]] != rk[sa[i]]);
    memcpy(rk.data(), id.data(), sizeof(int) * n);
    for (int j = 1; j < n && pre != n - 1; j <<= 1) {
      int cur = 0;
      for (int i = n - j; i < n; i++) id[cur++] = i;
      for (int i = 0; i < n; i++)
        if (sa[i] >= j) id[cur++] = sa[i] - j;
      memset(buc.data(), 0, sizeof(int) * n);
      for (int i = 0; i < n; i++) buc[rk[i]] += 1;
      for (int i = 1; i < n; i++) buc[i] += buc[i - 1];
      for (int i = n; i--;) sa[--buc[rk[id[i]]]] = id[i];
      id[0] = 0;
      auto pred = [&](int x, int y) {
        return rk[x] != rk[y] || rk[x + j] != rk[y + j];
      };
      pre = id[sa[0]] = 0;
      for (int i = 1; i < n; i++) id[sa[i]] = (pre += pred(sa[i - 1], sa[i]));
      memcpy(rk.data(), id.data(), sizeof(int) * n);
    }
    auto& lcp = st[0];
    lcp.resize(n - 1);
    for (int i = 0, h = 0; i < n; i++) {
      if (!rk[i]) continue;
      if (h) h -= 1;
      int j = sa[rk[i] - 1];
      while (max(i, j) + h < n && str[i + h] == str[j + h]) h += 1;
      lcp[rk[i] - 1] = h;
    }
    for (int j = 1; 1 << j <= n; j++) {
      st[j].resize(n - (1 << j));
      for (int i = 0; i + (1 << j) < n; i++) {
        st[j][i] = min(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);
      }
    }
  }
  int operator()(int i, int j) const {
    if (max(i, j) == n) return 0;
    if (i == j) return n - i;
    int l = min(rk[i], rk[j]), r = max(rk[i], rk[j]) - 1;
    int k = __lg(r - l + 1);
    return min(st[k][l], st[k][r - (1 << k) + 1]);
  }
};// }}}
int main() {
#ifndef LOCAL
  cin.tie(nullptr)->sync_with_stdio(false);
#endif
  string s;
  cin >> s;
  machine mac{s};
  for (int i = 0; i < (int)s.size(); i++)
    cout << 1 + mac.sa[i] << " \n"[i + 1 == (int)s.size()];
  for (int i = 0; i +1< (int)s.size(); i++)
    cout <<  mac.st[0][i] << " \n"[i +2 == (int)s.size()];
if (s.size() == 1) cout << endl;
  return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Subtask #1 Testcase #149.05 us60 KBAcceptedScore: 100

Subtask #1 Testcase #245.09 us60 KBAcceptedScore: 0

Subtask #1 Testcase #341.84 us60 KBAcceptedScore: 0

Subtask #1 Testcase #445.13 us64 KBAcceptedScore: 0

Subtask #1 Testcase #544.73 us64 KBAcceptedScore: 0

Subtask #1 Testcase #647.13 us64 KBAcceptedScore: 0

Subtask #1 Testcase #723.315 ms8 MB + 560 KBAcceptedScore: 0

Subtask #1 Testcase #839.234 ms8 MB + 748 KBAcceptedScore: 0

Subtask #1 Testcase #925.471 ms8 MB + 612 KBAcceptedScore: 0

Subtask #1 Testcase #1016.61 ms5 MB + 524 KBAcceptedScore: 0

Subtask #1 Testcase #1117.566 ms5 MB + 564 KBAcceptedScore: 0

Subtask #1 Testcase #1232.413 ms8 MB + 940 KBAcceptedScore: 0

Subtask #1 Testcase #1331.027 ms8 MB + 908 KBAcceptedScore: 0

Subtask #1 Testcase #1426.836 ms8 MB + 640 KBAcceptedScore: 0

Subtask #1 Testcase #1526.597 ms8 MB + 656 KBAcceptedScore: 0

Subtask #1 Testcase #1631.335 ms8 MB + 940 KBAcceptedScore: 0

Subtask #1 Testcase #1731.525 ms8 MB + 940 KBAcceptedScore: 0

Subtask #1 Testcase #1831.893 ms8 MB + 940 KBAcceptedScore: 0


Judge Duck Online | 评测鸭在线
Server Time: 2024-11-22 18:10:11 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠