QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#278130#7876. Cyclic SubstringswuyunAC ✓259ms622648kbC++202.3kb2023-12-07 12:40:492023-12-07 12:40:49

Judging History

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

  • [2023-12-07 12:40:49]
  • 评测
  • 测评结果:AC
  • 用时:259ms
  • 内存:622648kb
  • [2023-12-07 12:40:49]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

constexpr i64 p = 998244353;

struct PAM {
    const static int S = 10;
    struct Node {
        int len, link;
        int cnt {};
        array<int, S> nxt {};
    };
    int last;
    string s;
    vector<Node> t;
    int newNode(int len, int link) {
        t.emplace_back();
        t.back() = {len, link};
        return (int)t.size() - 1;
    }
    PAM() : last(1), s(1, ' ') {
        newNode(0, 1);
        newNode(-1, 1);
    }
    int jump(int u) { // p指向要匹配的字符的位置, u为初始开始匹配节点位置
        int p = (int)s.size() - 1;
        while (s[p - t[u].len - 1] != s[p]) {
            u = t[u].link;
        }
        return u;
    }
    int insert(char ch) {
        s.push_back(ch);
        int u = jump(last);
        int v = t[u].nxt[ch - '0'];
        if (!v) {
            int link = t[jump(t[u].link)].nxt[ch - '0'];
            v = t[u].nxt[ch - '0'] = newNode(t[u].len + 2, link);
        }
        last = v;
        t[v].cnt++;
        return last;
    }
};

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    int n;
    cin >> n;
    string s;
    cin >> s;
    s += s;
    s.insert(0, " ");

    PAM pam;
    for (int i = 1; i <= n; i++) {
        pam.insert(s[i]);
    }

    auto nodes = pam.t;
    vector<i64> temp(nodes.size());
    for (int i = (int)nodes.size() - 1; i >= 2; i--) {
        temp[i] += nodes[i].cnt;
        temp[i] %= ::p;
        temp[nodes[i].link] += temp[i];
        temp[nodes[i].link] %= ::p;
    }
    
    for (int i = n + 1; i <= 2 * n; i++) {
        pam.insert(s[i]);
    }
    vector<i64> cnt(pam.t.size());
    for (int i = (int)pam.t.size() - 1; i >= 2; i--) {
        cnt[i] += pam.t[i].cnt;
        cnt[pam.t[i].link] += cnt[i];
        cnt[i] %= ::p;
        cnt[pam.t[i].link] %= ::p;
    }

    i64 ans = 0;
    for (int i = 2; i < (int)cnt.size(); i++) {
        if (pam.t[i].len > n) continue;
        if (i < (int)temp.size()) {
            cnt[i] -= temp[i];
            cnt[i] = (cnt[i] + ::p) % ::p;
        }
        ans += cnt[i] * cnt[i] % ::p * pam.t[i].len;
        ans %= ::p;
    }

    cout << ans << "\n";



    return 0;
}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

5
01010

output:

39

result:

ok 1 number(s): "39"

Test #2:

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

input:

8
66776677

output:

192

result:

ok 1 number(s): "192"

Test #3:

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

input:

1
1

output:

1

result:

ok 1 number(s): "1"

Test #4:

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

input:

2
22

output:

12

result:

ok 1 number(s): "12"

Test #5:

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

input:

2
21

output:

2

result:

ok 1 number(s): "2"

Test #6:

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

input:

3
233

output:

10

result:

ok 1 number(s): "10"

Test #7:

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

input:

3
666

output:

54

result:

ok 1 number(s): "54"

Test #8:

score: 0
Accepted
time: 81ms
memory: 186752kb

input:

1000000
3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333...

output:

496166704

result:

ok 1 number(s): "496166704"

Test #9:

score: 0
Accepted
time: 259ms
memory: 622648kb

input:

3000000
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222...

output:

890701718

result:

ok 1 number(s): "890701718"

Test #10:

score: 0
Accepted
time: 163ms
memory: 318900kb

input:

3000000
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...

output:

224009870

result:

ok 1 number(s): "224009870"

Test #11:

score: 0
Accepted
time: 212ms
memory: 622620kb

input:

3000000
8989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989...

output:

51985943

result:

ok 1 number(s): "51985943"

Test #12:

score: 0
Accepted
time: 173ms
memory: 622500kb

input:

3000000
1911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911...

output:

355676465

result:

ok 1 number(s): "355676465"

Test #13:

score: 0
Accepted
time: 215ms
memory: 589216kb

input:

3000000
7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777...

output:

788510374

result:

ok 1 number(s): "788510374"

Test #14:

score: 0
Accepted
time: 207ms
memory: 592184kb

input:

3000000
5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555...

output:

691884476

result:

ok 1 number(s): "691884476"

Test #15:

score: 0
Accepted
time: 148ms
memory: 374056kb

input:

3000000
0990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990...

output:

701050848

result:

ok 1 number(s): "701050848"

Test #16:

score: 0
Accepted
time: 67ms
memory: 120312kb

input:

3000000
2772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772...

output:

486861605

result:

ok 1 number(s): "486861605"

Test #17:

score: 0
Accepted
time: 198ms
memory: 568524kb

input:

3000000
4554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554...

output:

450625621

result:

ok 1 number(s): "450625621"

Test #18:

score: 0
Accepted
time: 163ms
memory: 561160kb

input:

3000000
1181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181...

output:

649551870

result:

ok 1 number(s): "649551870"

Extra Test:

score: 0
Extra Test Passed