QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#163106#5084. Longest Substringstd_absWA 112ms28172kbC++203.1kb2023-09-03 20:53:122023-09-03 20:53:13

Judging History

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

  • [2023-09-03 20:53:13]
  • 评测
  • 测评结果:WA
  • 用时:112ms
  • 内存:28172kb
  • [2023-09-03 20:53:12]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
#define pb push_back
#define all(a) a.begin(), a.end()
#define sz(a) ((int)a.size())
const int mod = 998244353, N = 100005;

struct SAM {
  int ch[N][26], len[N], link[N], pos[N], cnt[N], sz;
  // node -> strings with the same endpos set
  // length in range [len(link) + 1, len]
  // node's endpos set -> pos in the subtree of node
  // link -> longest suffix with different endpos set
  // len  -> longest suffix
  // pos  -> end position
  // cnt  -> size of endpos set
  SAM () {len[0] = 0, link[0] = -1, pos[0] = 0, cnt[0] = 0, sz = 1;}
  void build(string s) {
    int last = 0;
    for (int i = 0; i < s.length(); ++i) {
      char c = s[i];
      int cur = sz++;
      len[cur] = len[last] + 1, pos[cur] = i + 1;
      int p = last;
      while (~p && !ch[p][c - 'a'])
        ch[p][c - 'a'] = cur, p = link[p];
      if (p == -1) link[cur] = 0;
      else {
        int q = ch[p][c - 'a'];
        if (len[p] + 1 == len[q]) {
          link[cur] = q;
        } else {
          int nxt = sz++;
          len[nxt] = len[p] + 1, link[nxt] = link[q];
          pos[nxt] = 0;
          for (int j = 0; j < 26; ++j)
            ch[nxt][j] = ch[q][j];
          while (~p && ch[p][c - 'a'] == q)
            ch[p][c - 'a'] = nxt, p = link[p];
          link[q] = link[cur] = nxt;
        }
      }
      cnt[cur]++;
      last = cur;
    }
    vector <int> p(sz);
    iota(all(p), 0);
    sort(all(p),
      [&](int i, int j) {return len[i] > len[j];});
    for (int i = 0; i < sz; ++i)
      cnt[link[p[i]]] += cnt[p[i]];
  }
} sam;

vector <int> adj[N];
set <int> pos[N];
int ans[N], sz[N];

int query(int v, int len) {
    int ans = 0;
    auto it = pos[v].lower_bound(0);
    while (it != pos[v].end()) {
        ans++;
        int x = *it;
        it = pos[v].lower_bound(x + len);
    }
    return ans;
}

void dfs(int v) {
    int mx = -1;
    for (int u : adj[v]) {
        dfs(u);
        sz[v] += sz[u];
        if (mx == -1 || sz[u] > sz[mx]) {
            mx = u;
        }
    }
    if (mx != -1) {
        swap(pos[mx], pos[v]);
    }
    for (int u : adj[v]) {
        for (int j : pos[u]) {
            pos[v].insert(j);
        }
        pos[u].clear();
    }
    if (v) {
        int cnt = sam.cnt[v];
        assert(pos[v].size() == cnt);
        int l = sam.len[sam.link[v]] + 1, r = sam.len[v] + 1;
        int x = query(v, l);
        while (r - l > 1) {
            int m = l + r >> 1;
            if (query(v, m) == x) {
                l = m;
            } else {
                r = m;
            }
        }
        ans[cnt] = max(ans[cnt], l);
    }
}

int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    string s; cin >> s;
    int n = s.length();
    sam.build(s);
    for (int i = 1; i < sam.sz; ++i) {
        adj[sam.link[i]].pb(i);
        if (sam.pos[i] > 0) {
            pos[i].insert(sam.pos[i]), sz[i] = 1;
        }
    }
    dfs(0);
    for (int i = 1; i <= n; ++i) {
        cout << ans[i] << " \n"[i == n];
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 14096kb

input:

ababa

output:

5 2 1 0 0

result:

ok single line: '5 2 1 0 0'

Test #2:

score: 0
Accepted
time: 0ms
memory: 13012kb

input:

aaaaaaaa

output:

8 7 6 5 4 3 2 1

result:

ok single line: '8 7 6 5 4 3 2 1'

Test #3:

score: 0
Accepted
time: 1ms
memory: 12900kb

input:

a

output:

1

result:

ok single line: '1'

Test #4:

score: 0
Accepted
time: 2ms
memory: 12456kb

input:

abcdefghijklmnopqrstuvwxyz

output:

26 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

result:

ok single line: '26 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'

Test #5:

score: 0
Accepted
time: 73ms
memory: 28172kb

input:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

50000 49999 49998 49997 49996 49995 49994 49993 49992 49991 49990 49989 49988 49987 49986 49985 49984 49983 49982 49981 49980 49979 49978 49977 49976 49975 49974 49973 49972 49971 49970 49969 49968 49967 49966 49965 49964 49963 49962 49961 49960 49959 49958 49957 49956 49955 49954 49953 49952 49951 ...

result:

ok single line: '50000 49999 49998 49997 49996 ...4 13 12 11 10 9 8 7 6 5 4 3 2 1'

Test #6:

score: -100
Wrong Answer
time: 112ms
memory: 26096kb

input:

abababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababababab...

output:

50000 49998 49996 49994 49992 49990 49988 49986 49984 49982 49980 49978 49976 49974 49972 49970 49968 49966 49964 49962 49960 49958 49956 49954 49952 49950 49948 49946 49944 49942 49940 49938 49936 49934 49932 49930 49928 49926 49924 49922 49920 49918 49916 49914 49912 49910 49908 49906 49904 49902 ...

result:

wrong answer 1st lines differ - expected: '50000 49998 49996 49994 49992 ...0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0', found: '50000 49998 49996 49994 49992 ...0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'