提交记录 27336


用户 题目 状态 得分 用时 内存 语言 代码长度
caijianhong 1006. 【模板题】后缀排序 Wrong Answer 0 39.39 ms 9132 KB C++ 2.44 KB
提交时间 评测时间
2024-11-15 21:27:08 2024-11-15 21:27:14
#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()];
  return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Subtask #1 Testcase #151.72 us60 KBWrong AnswerScore: 0

Subtask #1 Testcase #242.86 us60 KBAcceptedScore: 0

Subtask #1 Testcase #341.13 us60 KBAcceptedScore: 0

Subtask #1 Testcase #445.6 us64 KBAcceptedScore: 0

Subtask #1 Testcase #545.24 us64 KBAcceptedScore: 0

Subtask #1 Testcase #645.78 us64 KBAcceptedScore: 0

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

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

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

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

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

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

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

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

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

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

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

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


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