QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#658081#7876. Cyclic SubstringsH_ZzZWA 3ms21984kbC++203.1kb2024-10-19 16:07:162024-10-19 16:07:22

Judging History

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

  • [2024-10-19 16:07:22]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:21984kb
  • [2024-10-19 16:07:16]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 998244353;
constexpr int MAXN = 6e6+ 50;
int nnn;
namespace pam {
    int sz, tot, last;
    int cnt[MAXN], ch[MAXN][10], len[MAXN], fail[MAXN];
    char s[MAXN];

    int node(int l) {  // 建立一个新节点,长度为 l
        sz++;
        memset(ch[sz], 0, sizeof(ch[sz]));
        len[sz] = l;
        fail[sz] = cnt[sz] = 0;
        return sz;
    }

    void clear() {  // 初始化
        sz = -1;
        last = 0;
        s[tot = 0] = '$';
        node(0);
        node(-1);
        fail[0] = 1;
    }

    int getfail(int x) {  // 找后缀回文
        while (s[tot - len[x] - 1] != s[tot]) x = fail[x];
        return x;
    }

    void insert(char c) {  // 建树
        s[++tot] = c;
        int now = getfail(last);
        if (!ch[now][c - '0']) {
            int x = node(len[now] + 2);
            fail[x] = ch[getfail(fail[now])][c - '0'];
            ch[now][c - '0'] = x;
        }
        last = ch[now][c - '0'];
        cnt[last]++;
    }

    void solve() {
        long long ans = 0;
        for (int i = sz; i >= 0; i--) {
            cnt[fail[i]] += cnt[i];
        }

    }
}  // namespace pam

namespace pam2 {
    int sz, tot, last;
    int cnt[MAXN], ch[MAXN][10], len[MAXN], fail[MAXN];
    char s[MAXN];

    int node(int l) {  // 建立一个新节点,长度为 l
        sz++;
        memset(ch[sz], 0, sizeof(ch[sz]));
        len[sz] = l;
        fail[sz] = cnt[sz] = 0;
        return sz;
    }

    void clear() {  // 初始化
        sz = -1;
        last = 0;
        s[tot = 0] = '$';
        node(0);
        node(-1);
        fail[0] = 1;
    }

    int getfail(int x) {  // 找后缀回文
        while (s[tot - len[x] - 1] != s[tot]) x = fail[x];
        return x;
    }

    void insert(char c) {  // 建树
        s[++tot] = c;
        int now = getfail(last);
        if (!ch[now][c - '0']) {
            int x = node(len[now] + 2);
            fail[x] = ch[getfail(fail[now])][c - '0'];
            ch[now][c - '0'] = x;
        }
        last = ch[now][c - '0'];
        cnt[last]++;
    }
    void ssolve()
    {
        for (int i = sz; i >= 0; i--) {
            cnt[fail[i]] += cnt[i];
        }
    }
    long long solve() {
        long long ans = 0;
        for (int i = 2; i <= sz; i++) {  // 更新答案
            if(len[i] > nnn) break;
            ans += ((len[i])%mod * (cnt[i]* cnt[i])%mod)%mod;
            ans %= mod;
        }
        return ans%mod;
    }
}
string s;

signed main() {

    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cin >> nnn;
    cin >> s;
    string ss = s + s;
    pam::clear();
    pam2::clear();
    for (char i : s) {
        pam::insert(i);
    }

    for(char s : ss)
    {
        pam2::insert(s);
    }

    pam2::ssolve();
    pam::solve();
    for(int i = 2 ;i <= pam::sz; ++ i)
    {
        pam2::cnt[i] -= pam::cnt[i];
    }
    cout << pam2::solve() << '\n';

    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 21964kb

input:

5
01010

output:

39

result:

ok 1 number(s): "39"

Test #2:

score: -100
Wrong Answer
time: 3ms
memory: 21984kb

input:

8
66776677

output:

160

result:

wrong answer 1st numbers differ - expected: '192', found: '160'