QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#163103#5084. Longest Substringstd_absWA 127ms27580kbC++203.2kb2023-09-03 20:51:262023-09-03 20:51:27

Judging History

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

  • [2023-09-03 20:51:27]
  • 评测
  • 测评结果:WA
  • 用时:127ms
  • 内存:27580kb
  • [2023-09-03 20:51:26]
  • 提交

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;
    SAM () {
        len[0] = 0, link[0] = -1, pos[0] = 0, cnt[0] = 0, sz = 1;
    }
    void build(string s) {
        int lst = 0;
        for (int i = 0; i < s.length(); ++i) {
            char c = s[i];
            int cur = sz++;
            len[cur] = len[lst] + 1, pos[cur] = i + 1;
            int p = lst;
            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]++;
            lst = 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] << " ";
    }
    cout << "\n";
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 12272kb

input:

ababa

output:

5 2 1 0 0 

result:

ok single line: '5 2 1 0 0 '

Test #2:

score: 0
Accepted
time: 3ms
memory: 13736kb

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: 13980kb

input:

a

output:

1 

result:

ok single line: '1 '

Test #4:

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

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: 77ms
memory: 27580kb

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 ... 13 12 11 10 9 8 7 6 5 4 3 2 1 '

Test #6:

score: -100
Wrong Answer
time: 127ms
memory: 26648kb

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 '