QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#71841#3293. 优秀的拆分He_Ren100 ✓95ms8676kbC++233.9kb2023-01-12 01:48:182023-01-12 01:48:22

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-01-12 01:48:22]
  • 评测
  • 测评结果:100
  • 用时:95ms
  • 内存:8676kb
  • [2023-01-12 01:48:18]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MAXN = 3e4 + 5;
const int LB = 14 + 2;
const int inf = 0x3f3f3f3f;

int lb[MAXN];

struct SA {
    int rnk1[MAXN], rnk2[MAXN];
    int sa[MAXN], *rnk, *lst_rnk, st[MAXN][LB], sa_k;
    SA(void): rnk(rnk1), lst_rnk(rnk2) {}
    inline bool sa_same(int x, int y) {
        return lst_rnk[x] == lst_rnk[y] && lst_rnk[x + sa_k] == lst_rnk[y + sa_k];
    }
    inline void build(char s[], int n) {
        static int t[MAXN];
        int m = 3e2;
        memset(t, 0, (m + 1) << 2);

        for (int i = 1; i <= n; ++i)
            ++t[rnk[i] = s[i]];

        for (int i = 1; i <= m; ++i)
            t[i] += t[i - 1];

        for (int i = n; i >= 1; --i)
            sa[t[rnk[i]]--] = i;

        rnk[0] = lst_rnk[0] = rnk[n + 1] = lst_rnk[n + 1] = -1;

        if (n == 1)
            rnk[1] = 1;

        if (m == n)
            ++m;

        for (int &k = sa_k = 1; m != n; k <<= 1) {
            static int id[MAXN];
            int *w = lst_rnk, cur = 0;

            for (int i = n - k + 1; i <= n; ++i)
                id[++cur] = i;

            for (int i = 1; i <= n; ++i)
                if (sa[i] > k)
                    id[++cur] = sa[i] - k;

            memset(t, 0, (m + 1) << 2);

            for (int i = 1; i <= n; ++i)
                ++t[w[i] = rnk[id[i]]];

            for (int i = 1; i <= m; ++i)
                t[i] += t[i - 1];

            for (int i = n; i >= 1; --i)
                sa[t[w[i]]--] = id[i];

            m = 0;
            swap(lst_rnk, rnk);

            for (int i = 1; i <= n; ++i)
                rnk[sa[i]] = sa_same(sa[i], sa[i - 1]) ? m : ++m;
        }

        for (int i = 1, k = 0; i <= n; ++i) {
            if (rnk[i] == n) {
                st[n][0] = k = 0;
                continue;
            }

            if (k)
                --k;

            int j = sa[rnk[i] + 1];

            while (s[i + k] == s[j + k])
                ++k;

            st[rnk[i]][0] = k;
        }

        for (int i = n; i >= 1; --i)
            for (int j = 1; i + (1 << j) - 1 <= n; ++j)
                st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
    }
    inline int get_min(int l, int r) {
        int k = lb[r - l + 1];
        return min(st[l][k], st[r - (1 << k) + 1][k]);
    }
    inline int lcp(int x, int y) {
        if (x == y)
            return inf;

        x = rnk[x];
        y = rnk[y];
        return x < y ? get_min(x, y - 1) : get_min(y, x - 1);
    }
    inline int operator [](const int x) const {
        return sa[x];
    }
} pre_sa, suf_sa;

int n;
char s[MAXN];
int f[MAXN], g[MAXN];

inline void build(void) {
    suf_sa.build(s, n);
    reverse(s + 1, s + n + 1);
    pre_sa.build(s, n);
    reverse(s + 1, s + n + 1);
}
inline int lcs(int x, int y) {
    return pre_sa.lcp(n - x + 1, n - y + 1);
}
inline int lcp(int x, int y) {
    return suf_sa.lcp(x, y);
}

inline void upd(int a[], int l, int r) {
    if (l <= r)
        ++a[l], --a[r + 1];
}

void solve(void) {
    scanf("%s", s + 1);
    n = strlen(s + 1);

    build();

    for (int i = 1; i <= n + 1; ++i)
        f[i] = g[i] = 0;

    for (int k = 1; k <= n; ++k)
        for (int i = 1; i + k <= n; i += k) {
            int j = i + k;
            int l = i - min(lcs(i, j), k) + 1, r = j + min(lcp(i, j), k) - 1;
            upd(g, l, r - 2 * k + 1);
            upd(f, l + 2 * k - 1, r);
        }

    for (int i = 1; i <= n; ++i)
        f[i] += f[i - 1], g[i] += g[i - 1];

    ll ans = 0;

    for (int i = 1; i < n; ++i)
        ans += (ll)f[i] * g[i + 1];

    printf("%lld\n", ans);
}

int main(void) {
    lb[0] = -1;

    for (int i = 2; i < MAXN; ++i)
        lb[i] = lb[i >> 1] + 1;

    int T;
    scanf("%d", &T);

    while (T--)
        solve();

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 5
Accepted
time: 0ms
memory: 7780kb

input:

10
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk...

output:

462056
140600
875978
575960
811035
299536
173880
414090
227128
924490

result:

ok 10 numbers

Test #2:

score: 5
Accepted
time: 3ms
memory: 5872kb

input:

10
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq...

output:

369564
513590
285760
308945
408312
190568
180441
328350
437635
1091574

result:

ok 10 numbers

Test #3:

score: 5
Accepted
time: 7ms
memory: 5964kb

input:

10
kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk...

output:

92601930
126763350
233408728
111659395
147774025
41791750
62056280
198982282
172593608
298613460

result:

ok 10 numbers

Test #4:

score: 5
Accepted
time: 6ms
memory: 7832kb

input:

10
nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn...

output:

241383298
63369600
53220335
75846485
53073182
47276061
287600152
128609208
205431400
71461299

result:

ok 10 numbers

Test #5:

score: 5
Accepted
time: 3ms
memory: 5840kb

input:

10
nnnnnnnnnn
hzhzhzhzhz
zezezezeze
ndndndndnd
zvzvzvzvzv
bbbbbbbbbb
sgsgsgsgsg
fgfgfgfgfg
hxhxhxhxhx
fafafafafa

output:

30
3
3
3
3
30
3
3
3
3

result:

ok 10 numbers

Test #6:

score: 5
Accepted
time: 3ms
memory: 5868kb

input:

10
xxxxxxxxxx
wtwtwtwtwt
sososososo
xgxgxgxgxg
lblblblblb
rororororo
qfqfqfqfqf
qjqjqjqjqj
upupupupup
nynynynyny

output:

30
3
3
3
3
3
3
3
3
3

result:

ok 10 numbers

Test #7:

score: 5
Accepted
time: 1ms
memory: 8040kb

input:

10
jjjjjjjjjjjjjjjjjjjj
tgtgtgtgtg
mcomcomcomco
wthwthwthwth
aaitaaitaaitaait
pvaompvaompvaompvaom
ahahahahah
jwxmjwxmjwxmjwxm
pdfpdfpdfpdf
oiyfoiyfoiyfoiyf

output:

285
3
1
1
5
1
3
1
1
1

result:

ok 10 numbers

Test #8:

score: 5
Accepted
time: 0ms
memory: 5792kb

input:

10
iiiiiiiiiiiiiiiii
scscscscscscsc
bsobsobsobsobso
rppyrppyrppyrppy
xhnxhnxhnxhn
jfbjfbjfbjfb
tqtqtqtqtq
cdocdocdocdocdo
imgimgimgimg
zxzxzxzxzx

output:

168
13
4
5
1
1
3
4
1
3

result:

ok 10 numbers

Test #9:

score: 5
Accepted
time: 3ms
memory: 5868kb

input:

10
sssssssssssssssssssss
wawawawawawawawa
xlexlexlexlexlexlexle
cwqfcwqfcwqfcwqfcwqfcwqfcwqf
dappidappidappidappidappi
biwtbbiwtbbiwtbbiwtb
ipdbipdbipdbipdb
oyqjoyqjoyqjoyqj
miqozmiqozmiqozmiqoz
ofqgofqgofqgofqg

output:

330
22
18
23
14
3
1
1
1
1

result:

ok 10 numbers

Test #10:

score: 5
Accepted
time: 3ms
memory: 6020kb

input:

10
zzzzzzzzzzzzzzzzzzzzzzzzzzz
icicicicicicicicicicic
saasaasaasaasaasaasaa
znunznunznunznunznun
ttfhhttfhhttfhhttfhhttfhh
fqxqblfqxqblfqxqblfqxqblfqxqbl
xxpruxxpruxxpruxxpru
mpheqsmpheqsmpheqsmpheqs
ptvbemqptvbemqptvbemqptvbemq
nxykqknxykqknxykqknxykqk

output:

728
70
36
5
26
7
5
1
1
1

result:

ok 10 numbers

Test #11:

score: 5
Accepted
time: 0ms
memory: 5860kb

input:

10
jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
ibibibibibibibibibibibibibibibibibibibibibibibib
xinxinxinxinxinxinxinxinxinxinxin
ivlwivlwivlwivlwivlwivlw
cxrvucxrvucxrvucxrvucxrvucxrvucxrvucxrvucxrvu
jzqfmgjzqfmgjzqfmgjzqfmgjzqfmg
nqctlysnqctlysnqctlysnqctlysnqctlysnqctlysnqctlys
kuizuntnkuizuntnkuizuntnkuizu...

output:

1240
946
100
11
76
7
38
9
18
1

result:

ok 10 numbers

Test #12:

score: 5
Accepted
time: 0ms
memory: 7840kb

input:

10
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
dvdvdvdvdvdvdvdvdvdvdvdvdv
jhyjhyjhyjhyjhyjhyjhyjhyjhy
dkbjdkbjdkbjdkbjdkbjdkbjdkbjdkbj
ezlbtezlbtezlbtezlbtezlbtezlbtezlbtezlbt
efwduoefwduoefwduoefwduoefwduoefwduoefwduo
eqgdlgxeqgdlgxeqgdlgxeqgdlgxeqgdlgxeqgdlgx
mwuoqleamwuoqleamwuoqleamwuoqlea
gwcbhodfpgwcb...

output:

1632
125
48
38
46
33
17
1
1
5

result:

ok 10 numbers

Test #13:

score: 5
Accepted
time: 3ms
memory: 5836kb

input:

10
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
xwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxw
rslrslrslrslrslrslrslrslrslrslrslrslrslrslrslrslrslrslrslrslrslrslrslrsl
iimeiimeiimeiimeiimeiimeiimeiimeiimeiimeiimeiime...

output:

25585
6391
1386
1014
876
567
62
118
33
86

result:

ok 10 numbers

Test #14:

score: 5
Accepted
time: 1ms
memory: 5836kb

input:

10
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
ozozozozozozozozozozozozozozozozozozozozozozozozozozozozozozozoz
ysvysvysvysvysvysvysvysvysvysvysvysvysvysvysvysvysvysv
ipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbd
x...

output:

19019
2360
540
1826
110
7
511
118
132
53

result:

ok 10 numbers

Test #15:

score: 5
Accepted
time: 1ms
memory: 7884kb

input:

10
ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
vzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvz
yzkyzkyzkyzkyzk...

output:

155155
18445
7600
13475
3328
72
114
37
17
6

result:

ok 10 numbers

Test #16:

score: 5
Accepted
time: 3ms
memory: 5824kb

input:

10
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
bxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbx...

output:

281295
48503
26100
13475
20516
80
72
59
21
26

result:

ok 10 numbers

Test #17:

score: 5
Accepted
time: 1ms
memory: 7852kb

input:

10
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii...

output:

1391040
981179
345415
50677
52576
227
28
413
144
50

result:

ok 10 numbers

Test #18:

score: 5
Accepted
time: 4ms
memory: 5932kb

input:

10
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff...

output:

38263590
33623065
4070400
510708
624588
2102
1074
299
408
1538

result:

ok 10 numbers

Test #19:

score: 5
Accepted
time: 6ms
memory: 5944kb

input:

10
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq...

output:

245030555
46877978
4884100
14318590
12858978
5063
5381
7598
4729
5228

result:

ok 10 numbers

Test #20:

score: 5
Accepted
time: 95ms
memory: 8676kb

input:

10
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy...

output:

563349754956
161455324997
76621205738
70150901846
40842068960
6056659
2820346
3357795
2628223
10884

result:

ok 10 numbers

Extra Test:

score: 0
Extra Test Passed