QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#661382#7876. Cyclic SubstringsCalculatelove#AC ✓373ms780416kbC++141.9kb2024-10-20 15:59:182024-10-20 15:59:30

Judging History

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

  • [2024-10-20 15:59:30]
  • 评测
  • 测评结果:AC
  • 用时:373ms
  • 内存:780416kb
  • [2024-10-20 15:59:18]
  • 提交

answer

#include <bits/stdc++.h>

const int N = 3001000;
const int mod = 998244353;

int n;
char str[N];

namespace PAM {
    int cur_len, str[N * 2];

    int nClock, Last;
    struct node {
        int trans[10];
        int link, len;
    } t[N * 2];

    int cnt[N * 2];

    int tot, head[N * 2], ver[N * 2], Next[N * 2];
    void add_edge(int u, int v) {
        ver[++ tot] = v;    Next[tot] = head[u];    head[u] = tot;
    }

    void init() {
        t[0].len = 0, t[0].link = 1;
        t[1].len = -1;

        str[cur_len = 0] = -1;
        nClock = 1, Last = 1;
    }

    int find(int p) {
        while (str[cur_len - t[p].len - 1] != str[cur_len]) p = t[p].link;
        return p;
    }

    void extend(int c, int type) {
        str[++ cur_len] = c;

        int p = find(Last);
        if (!t[p].trans[c]) {
            int np = ++ nClock;

            t[np].len = t[p].len + 2;
            t[np].link = t[find(t[p].link)].trans[c];

            t[p].trans[c] = np;
        }

        Last = t[p].trans[c];

        if (type) cnt[Last] ++;
    }

    void build_tree() {
        add_edge(1, 0);
        for (int i = 2; i <= nClock; i ++) add_edge(t[i].link, i);
    }

    void dfs(int u) {
        for (int i = head[u]; i; i = Next[i]) {
            int v = ver[i];
            dfs(v);
            cnt[u] += cnt[v];
        }
    }

    int calc() {
        int ans = 0;
        for (int i = 2; i <= nClock; i ++)
            if (t[i].len <= n)
                ans = (ans + 1ll * cnt[i] * cnt[i] % mod * t[i].len) % mod;
        return ans;
    }
}

int main() {
    scanf("%d", &n);
    scanf("%s", str + 1);

    PAM::init();

    for (int i = 1; i <= n; i ++) PAM::extend(str[i] - '0', 0);
    for (int i = 1; i <= n; i ++) PAM::extend(str[i] - '0', 1);

    PAM::build_tree();
    PAM::dfs(1);

    printf("%d\n", PAM::calc());

    return 0;
}

这程序好像有点Bug,我给组数据试试?

詳細信息

Test #1:

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

input:

5
01010

output:

39

result:

ok 1 number(s): "39"

Test #2:

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

input:

8
66776677

output:

192

result:

ok 1 number(s): "192"

Test #3:

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

input:

1
1

output:

1

result:

ok 1 number(s): "1"

Test #4:

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

input:

2
22

output:

12

result:

ok 1 number(s): "12"

Test #5:

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

input:

2
21

output:

2

result:

ok 1 number(s): "2"

Test #6:

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

input:

3
233

output:

10

result:

ok 1 number(s): "10"

Test #7:

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

input:

3
666

output:

54

result:

ok 1 number(s): "54"

Test #8:

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

input:

1000000
3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333...

output:

496166704

result:

ok 1 number(s): "496166704"

Test #9:

score: 0
Accepted
time: 208ms
memory: 780416kb

input:

3000000
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222...

output:

890701718

result:

ok 1 number(s): "890701718"

Test #10:

score: 0
Accepted
time: 94ms
memory: 257176kb

input:

3000000
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...

output:

224009870

result:

ok 1 number(s): "224009870"

Test #11:

score: 0
Accepted
time: 199ms
memory: 592952kb

input:

3000000
8989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989...

output:

51985943

result:

ok 1 number(s): "51985943"

Test #12:

score: 0
Accepted
time: 183ms
memory: 530352kb

input:

3000000
1911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911...

output:

355676465

result:

ok 1 number(s): "355676465"

Test #13:

score: 0
Accepted
time: 152ms
memory: 430488kb

input:

3000000
7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777...

output:

788510374

result:

ok 1 number(s): "788510374"

Test #14:

score: 0
Accepted
time: 161ms
memory: 415216kb

input:

3000000
5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555...

output:

691884476

result:

ok 1 number(s): "691884476"

Test #15:

score: 0
Accepted
time: 147ms
memory: 221332kb

input:

3000000
0990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990...

output:

701050848

result:

ok 1 number(s): "701050848"

Test #16:

score: 0
Accepted
time: 60ms
memory: 91760kb

input:

3000000
2772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772...

output:

486861605

result:

ok 1 number(s): "486861605"

Test #17:

score: 0
Accepted
time: 373ms
memory: 341236kb

input:

3000000
4554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554...

output:

450625621

result:

ok 1 number(s): "450625621"

Test #18:

score: 0
Accepted
time: 336ms
memory: 335656kb

input:

3000000
1181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181...

output:

649551870

result:

ok 1 number(s): "649551870"

Extra Test:

score: 0
Extra Test Passed