QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#142219#5084. Longest Substring8BQube#WA 242ms40032kbC++205.0kb2023-08-18 18:08:332023-08-18 18:08:34

Judging History

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

  • [2023-08-18 18:08:34]
  • 评测
  • 测评结果:WA
  • 用时:242ms
  • 内存:40032kb
  • [2023-08-18 18:08:33]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define X first
#define Y second
#define ALL(v) v.begin(), v.end()
#define pb push_back
#define SZ(a) ((int)a.size())

const int N = 50005;
const int CNUM = 26;
const int INF = 1e9;

struct exSAM {
    int milen[N * 2], len[N * 2], link[N * 2];  
    int next[N * 2][CNUM], tot;
    int lenSorted[N * 2];
    int cnt[N * 2];
    int newnode() {
        fill_n(next[tot], CNUM, 0);
        len[tot] = cnt[tot] = link[tot] = 0;
        return tot++;
    }
    void init() { tot = 0, newnode(), link[0] = -1; }
    int insertSAM(int last, int c) {
        int cur = next[last][c];
        len[cur] = len[last] + 1;
        int p = link[last];
        while (p != -1 && !next[p][c])
            next[p][c] = cur, p = link[p];
        if (p == -1) return link[cur] = 0, cur;
        int q = next[p][c];
        if (len[p] + 1 == len[q]) return link[cur] = q, cur;
        int clone = newnode();
        for (int i = 0; i < CNUM; ++i)
            next[clone][i] = len[next[q][i]] ? next[q][i] : 0;
        len[clone] = len[p] + 1;
        while (p != -1 && next[p][c] == q)
            next[p][c] = clone, p = link[p];
        link[link[cur] = clone] = link[q];
        link[q] = clone;
        return cur;
    }
    void insert(const string &s) {
        int cur = 0;
        for (auto ch : s) {
            int &nxt = next[cur][int(ch - 'a')];
            if (!nxt) nxt = newnode();
            cnt[cur = nxt] += 1;
        }
    }
    void build() {
        queue<int> q;
        q.push(0);
        while (!q.empty()) {
            int cur = q.front();
            q.pop();
            for (int i = 0; i < CNUM; ++i)
                if (next[cur][i])
                    q.push(insertSAM(cur, i));
        }
        vector<int> lc(tot);
        for (int i = 1; i < tot; ++i) ++lc[len[i]];
        partial_sum(ALL(lc), lc.begin());
        for (int i = 1; i < tot; ++i) lenSorted[--lc[len[i]]] = i;
    }
    void solve() {
        for (int i = tot - 2; i >= 0; --i) {
            cnt[link[lenSorted[i]]] += cnt[lenSorted[i]];
        }
        fill_n(milen, tot, INF);
        for (int j = 0; j < CNUM; ++j)
            if (next[0][j])
                milen[next[0][j]] = 1;
        for (int i = 0; i <= tot - 2; ++i) {
            for (int j = 0; j < CNUM; ++j)
                if (next[lenSorted[i]][j]) {
                    milen[next[lenSorted[i]][j]] = min(milen[next[lenSorted[i]][j]], milen[lenSorted[i]] + 1);
                }
        }
    }
} sam;

vector<int> G[N * 2], buk[N * 2];
vector<int> seg[N * 4];
pii itv[N * 2], ans[N];
int in[N * 2], out[N * 2], pl[N * 2], dft;

void dfs(int u) {
    in[u] = ++dft, pl[dft] = u;
    for (int i : G[u])
        dfs(i);
    out[u] = dft;
}

void build(int l, int r, int rt) {
    for (int i = l; i <= r; ++i)
        for (int j : buk[pl[i]])
            seg[rt].pb(j);
    seg[rt].pb(INF);
    sort(ALL(seg[rt]));
    if (l == r) return;
    int mid = (l + r) >> 1;
    build(l, mid, rt << 1), build(mid + 1, r, rt << 1 | 1);
}

int lb(int L, int R, int l, int r, int rt, int v) {
    if (L <= l && R >= r) {
        return *lower_bound(ALL(seg[rt]), v);
    }
    int mid = (l + r) >> 1;
    if (R <= mid) return lb(L, R, l, mid, rt << 1, v);
    if (L > mid) return lb(L, R, mid + 1, r, rt << 1 | 1, v);
    return min(lb(L, R, l, mid, rt << 1, v), lb(L, R, mid + 1, r, rt << 1 | 1, v));
}

int sz = 0;

void solve(int u) {
    int l = itv[u].X, r = itv[u].Y + 1;
    
    auto jump = [&](int k) {
        int res = 0;
        auto p = lb(in[u], out[u], 1, sz, 1, 0);
        while (p < INF) {
            ++res;
            p = lb(in[u], out[u], 1, sz, 1, p + k); 
        }
        return res;
    };

    if (l != 0) {
        int mx = jump(l);

        while (r - l > 1) {
            int mid = (l + r) >> 1;
            if (jump(mid) == mx) l = mid;
            else r = mid;
        }

        //cerr << "solve " << u << ": " << mx << " " << l << ", cnt = " << sam.cnt[u] << "\n";
        ans[sam.cnt[u]] = max(ans[sam.cnt[u]], pii(mx, l));
    }

}

int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    string s;
    cin >> s;
    sam.init();
    sam.insert(s);
    sam.build();
    sam.solve();
    /*for (int i = 1; i < sam.tot; ++i) {
      cerr << sam.cnt[i] << " ";
      }
      cerr << endl;
      for (int i = 1; i < sam.tot; ++i) {
      cerr << sam.link[i] << " ";
      }
      cerr << endl;
      for (int i = 1; i < sam.tot; ++i) {
      cerr << "[" << sam.milen[i] << ", " << sam.len[i] << "] ";
      }
      cerr << endl;*/
    for (int i = 1; i < sam.tot; ++i) {
        itv[i] = pii(sam.milen[i], sam.len[i]);
        G[sam.link[i]].pb(i);
    }
    dfs(0);
    int cur = 0;
    for (int i = 0; i < SZ(s); ++i) {
        cur = sam.next[cur][int(s[i] - 'a')];
        buk[cur].pb(i);
    }
    sz = sam.tot;
    build(1, sz, 1);
    for (int i = 1; i < sam.tot; ++i)
        solve(i);
    for (int i = 1; i <= SZ(s); ++i)
        cout << ans[i].Y << " \n"[i == SZ(s)];
}

详细

Test #1:

score: 100
Accepted
time: 4ms
memory: 17808kb

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

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: 3ms
memory: 15776kb

input:

a

output:

1

result:

ok single line: '1'

Test #4:

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

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: 141ms
memory: 33192kb

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: 0
Accepted
time: 187ms
memory: 32480kb

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:

ok single line: '50000 49998 49996 49994 49992 ...0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'

Test #7:

score: 0
Accepted
time: 58ms
memory: 28296kb

input:

hnpyezhavuychdbjldxjofydbuekupcjllbrtehyfangipyjjqhivkyhnlrotjgftqhwlpvmsjtikgsspaxswleauvtzbmqjdywpnilqhawmcqprtynqylzbjganroyiwcbiyqklolxabdowwkrxhzuahgjabvvrkdelzvtdsavbipxpddsopnnxjwasoxnpgknyxrcuypmbgvgymrlymkcufnptcdwbpihxhayfqoylfvrgzhhferphxfjruejyztauvklpchuxduvosjrbwqvnofdnzhtkvwwbdenlzkyf...

output:

30918 6 4 3 3 3 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 0 2 2 0 2 2 0 0 0 0 0 0 2 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 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 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: '30918 6 4 3 3 3 3 3 0 0 0 0 0 ...0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'

Test #8:

score: 0
Accepted
time: 71ms
memory: 30656kb

input:

ujwgcpuvylongozaczorldxrlmgmdyxenplvanjgtgfgzdvllphwyuqvjmzvfskbmjndeazlljvcjwpgmbmhhnlppoahzjxzhlastuawhchrbajvfxgpxgrrzjjnufizgrofjnegdgrdsxnepoajtppsqpsjvscmekdwbsfecczcittiirastwwmhzrtjwksdlwchghosqslgniyxkvoukvbqhmfegaxrljefutidcelusbenpvuzwizoiznksyejikaovbngxmzzjrrumgdkvczjftegspfutgyayaxfozq...

output:

36879 5 4 3 3 3 3 3 3 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 0 2 0 0 2 0 0 0 2 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 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

result:

ok single line: '36879 5 4 3 3 3 3 3 3 0 3 0 0 ...0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'

Test #9:

score: 0
Accepted
time: 64ms
memory: 31560kb

input:

jrjefrpmcidljqjqbgcezgstiefshrvwcmtciarsnrnuucovzlltwvbmorxkwlrquidxnvejjedkendyssdtulgavoninkvcjzcpxsnnofaprgfclhxuezkbsezoefqgnnyxbvutgazsjttfftpjturngqoxpisvkpvkgypdpdwittmxnaaorxdkrldlrtatxwheabbyrezemqvymwzqffpysmqicopqxmsydemwzzalmjmjaewqqxwribmpfkafjmgsrpeimoulryfmmrxbvrhxatwoqrtaxqgboohpyqoe...

output:

32956 6 4 3 3 3 3 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 2 0 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 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 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 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 0 0 ...

result:

ok single line: '32956 6 4 3 3 3 3 3 3 0 0 0 0 ...0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'

Test #10:

score: 0
Accepted
time: 77ms
memory: 29156kb

input:

bqxywpdcsnccafvrbhsoffpezhdlywtbqpnqfkibmmvsacuohwzsrfmbvqhnqhhnvbkpzrofxssllygtwhxmbtxhvrnivshojgahurbgsnyvxgfdyzgqldgrbnutsakmruxlaadlqqiucwoumrhbkwdosgujwarnmgsjmsexfchydumtjapmngxzyxomsqqfxiksydtjvjnlimzjjeewbjmawzoqmkloamspatyraxnjdkncradykxajsqdbsbairguqqsaixggzrpavkporwnlbukavfsnzqsrxrjokttkn...

output:

37924 6 4 3 3 3 3 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 2 0 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 0 2 2 2 0 2 2 0 2 0 2 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 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 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

result:

ok single line: '37924 6 4 3 3 3 3 3 3 0 0 0 0 ...0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'

Test #11:

score: 0
Accepted
time: 42ms
memory: 26684kb

input:

bpgokflgellqalcfmnkaazoyenqztpllksopthmvpkmdgmjuhsdmawvgnxomrjpolstdymwkkjpxwxqeltezasdzatufflpyqrgtrkccierbjnrlguvpvyfrgvguivfdpcoankfoooewwulzzmbnltuysvvbethgnvdwpeusklobltcwvubybjtknhzpappqihgnziujksebunfncqgbqylpszscsvhuwmextkqlbxjktubotyzmhklohbesxujsduwreuayrhbsmsbimdlnzxllygzazsavtdejpdkhonch...

output:

21337 6 4 3 3 3 3 3 0 0 0 0 0 0 2 2 0 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 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 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 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 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: '21337 6 4 3 3 3 3 3 0 0 0 0 0 ...0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'

Test #12:

score: -100
Wrong Answer
time: 242ms
memory: 40032kb

input:

baaaabbababbabbaabbbabaabbababbbbababaaaabbbabaabaabaababbabaabbabaaaababbbbbaaabaaabbabaaabbbabbaababbbbabbbbabbabbbaabbaababbaabbabababbbbbbbabbaaaababbababbbbabbbaabbabbabaabaaaaaaababaaaaabbbbbabbaaaaabbabbbaabbbaabbabababbbaabbaaabaaaabaabaabaabaabbbbbbaabaabbaaaaaabababaabaabbaaaaababbbbbbbbba...

output:

3924 21 17 15 13 13 14 13 12 12 13 12 12 11 12 12 12 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 10 11 11 11 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 0 9 9 0 9 9 9 9 9 0 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9...

result:

wrong answer 1st lines differ - expected: '50000 30 22 19 18 17 16 15 14 ...0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0', found: '3924 21 17 15 13 13 14 13 12 1...0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'