QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#620358#7876. Cyclic SubstringswzxtslAC ✓477ms872764kbC++235.1kb2024-10-07 17:42:312024-10-07 17:42:32

Judging History

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

  • [2024-10-07 17:42:32]
  • 评测
  • 测评结果:AC
  • 用时:477ms
  • 内存:872764kb
  • [2024-10-07 17:42:31]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
// #define  long long long long
const  long long mod= 998244353;
long long sum=0;
 long long lens=0;
 long long cnc=0;
 long long nl[6000002]={0};
 long long nc[6000002]={0};
// long long bronya=0;
struct Node {
     long long len;                  // 节点表示的回文子串的长度
     long long link;                 // 后缀链接
   // map<char,  long long> next; // 从当前节点转移到的节点
     int next[11];
     long long cnt;   
             // 回文子串出现次数
};
vector<Node> nodes;      // 节点数组
 long long last;                     // 上一个字符结束的最大回文子串节点
 string s;                // 构建中的字符串
 long long k;                        // 限制的最大回文子串长度
 vector<Node> nodes1;
 long long last1;  
 string s1;                // 构建中的字符串
 long long k1;     
void init( long long max_len) {
    nodes.push_back({-1, 0, {}, 0}); // root for odd lengths
    nodes.push_back({ 0, 0, {}, 0}); // root for even lengths
    last = 1;
    s.push_back(-1); // -1 是一个虚拟字符,保证边界条件处理
    k = max_len;
}
void init1( long long max_len) {
    nodes1.push_back({-1, 0, {}, 0}); // root for odd lengths
    nodes1.push_back({ 0, 0, {}, 0}); // root for even lengths
    last1 = 1;
    s1.push_back(-1); // -1 是一个虚拟字符,保证边界条件处理
    k1 = max_len;
}

void add_letter(char letter) {
    //bronya++;
    s.push_back(letter);
     long long cur = s.size() - 1;

     long long p = last;
    while (s[cur - nodes[p].len - 1] != letter) {
        p = nodes[p].link;
    }
    
 
    if (nodes[p].next[letter-'0']) {
        last = nodes[p].next[letter-'0'];
  
        nodes[last].cnt++;
        return;
    }
     long long new_len = nodes[p].len + 2;

    last = nodes.size();
    if (new_len > k ) {
        last = p; // 更新 last 为当前节点
        return;   // 不添加超过限制的新节点
    }
    nodes.push_back({ new_len, 0, {}, 1});
    nodes[p].next[letter-'0'] = last;

    if (nodes[last].len == 1) {
        nodes[last].link = 1;
        return;
    }

    p = nodes[p].link;
    while (s[cur - nodes[p].len - 1] != letter) {
        p = nodes[p].link;
    }

    nodes[last].link = nodes[p].next[letter-'0'];
}

void add_letter1(char letter) {
    //bronya++;
    s1.push_back(letter);
     long long cur = s1.size() - 1;

     long long p = last1;
    while (s[cur - nodes1[p].len - 1] != letter) {
        p = nodes1[p].link;
    }
    
    //cout<<start_idx<<endl;
    if (nodes1[p].next[letter-'0']) {
        last1 = nodes1[p].next[letter-'0'];
        nodes1[last1].cnt++;
        return;
    }
     long long new_len = nodes1[p].len + 2;


    last1 = nodes1.size();
    if (new_len > k1 ) {
        last1 = p; // 更新 last 为当前节点
        return;   // 不添加超过限制的新节点
    }
    nodes1.push_back({ new_len, 0, {}, 1, });
    nodes1[p].next[letter-'0'] = last1;

    if (nodes1[last1].len == 1) {
        nodes1[last1].link = 1;
        return;
    }

    p = nodes1[p].link;
    while (s[cur - nodes1[p].len - 1] != letter) {
        p = nodes1[p].link;
    }

    nodes1[last1].link = nodes1[p].next[letter-'0'];
}

void count_all() {
    // 从后向前累加每个回文子串的出现次数到其后缀链接
    for ( long long i = nodes.size() - 1; i >= 2; --i) {
        nodes[nodes[i].link].cnt += nodes[i].cnt;
    }
}
void count_all1() {
    // 从后向前累加每个回文子串的出现次数到其后缀链接
    for ( long long i = nodes1.size() - 1; i >= 2; --i) {
        nodes1[nodes1[i].link].cnt += nodes1[i].cnt;
    }
}

void get_palindromes() {
    for ( long long i = 2; i <cnc; ++i) {
    //   cout<< nodes[i].cnt<<" "<<nodes1[i].cnt<<endl;
      //nodes[i].len <= lens&&nodes[i].start_idx <= lens&&
        if (nl[i]<=lens&&i<nodes1.size()) {
            sum=(sum%mod+nl[i]%mod*(nc[i]-nodes1[i].cnt)%mod*(nc[i]-nodes1[i].cnt)%mod)%mod;
         //   cout << "Length: " << nodes[i].len << ", Count: " << nodes[i].cnt <<  endl;
        }
        //if (nodes[i].len <= lens&&nodes[i].start_idx <= lens) 
        else if(nl[i]<=lens){
            sum=(sum%mod+nl[i]%mod*(nc[i])%mod*(nc[i])%mod)%mod;
         //   cout << "Length1: " << nodes[i].len << ", Count1: " << nodes[i].cnt <<  endl;
        }
    }
    cout<<sum<<endl;
}


int  main() {
   // k=5;
     long long n;
    cin>>n;
    string s_input ;cin>>s_input;
    string splus=s_input+s_input;
     long long max_length = n+n;
    lens=n;
    init(max_length);
    for (char c : splus) {
        add_letter(c);
    }
    count_all();
    cnc=nodes.size();
    for( long long i=2;i<nodes.size();i++){
       nl[i]=nodes[i].len;
       nc[i]=nodes[i].cnt;
    } 
    nodes.clear();
    init1(lens);
    for (char c : s_input) {
        add_letter1(c);
    }
    count_all1();
    get_palindromes();
  
   nodes1.clear();
    return 0;
}
//aba  abaaba
//abb  abbabb
//01010 0101001010

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

5
01010

output:

39

result:

ok 1 number(s): "39"

Test #2:

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

input:

8
66776677

output:

192

result:

ok 1 number(s): "192"

Test #3:

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

input:

1
1

output:

1

result:

ok 1 number(s): "1"

Test #4:

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

input:

2
22

output:

12

result:

ok 1 number(s): "12"

Test #5:

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

input:

2
21

output:

2

result:

ok 1 number(s): "2"

Test #6:

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

input:

3
233

output:

10

result:

ok 1 number(s): "10"

Test #7:

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

input:

3
666

output:

54

result:

ok 1 number(s): "54"

Test #8:

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

input:

1000000
3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333...

output:

496166704

result:

ok 1 number(s): "496166704"

Test #9:

score: 0
Accepted
time: 371ms
memory: 870500kb

input:

3000000
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222...

output:

890701718

result:

ok 1 number(s): "890701718"

Test #10:

score: 0
Accepted
time: 364ms
memory: 590160kb

input:

3000000
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...

output:

224009870

result:

ok 1 number(s): "224009870"

Test #11:

score: 0
Accepted
time: 375ms
memory: 871228kb

input:

3000000
8989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989898989...

output:

51985943

result:

ok 1 number(s): "51985943"

Test #12:

score: 0
Accepted
time: 410ms
memory: 872764kb

input:

3000000
1911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911911...

output:

355676465

result:

ok 1 number(s): "355676465"

Test #13:

score: 0
Accepted
time: 477ms
memory: 810168kb

input:

3000000
7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777...

output:

788510374

result:

ok 1 number(s): "788510374"

Test #14:

score: 0
Accepted
time: 453ms
memory: 828064kb

input:

3000000
5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555...

output:

691884476

result:

ok 1 number(s): "691884476"

Test #15:

score: 0
Accepted
time: 331ms
memory: 617924kb

input:

3000000
0990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990990909909909099090990990909909909099090990990909909099099090990990909909099099090990990909909099099090990909909909099099090990909909909099090990...

output:

701050848

result:

ok 1 number(s): "701050848"

Test #16:

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

input:

3000000
2772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772772727727727277272772772727727727277272772772727727277277272772772727727277277272772772727727277277272772727727727277277272772727727727277272772...

output:

486861605

result:

ok 1 number(s): "486861605"

Test #17:

score: 0
Accepted
time: 362ms
memory: 645900kb

input:

3000000
4554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554554545545545455454554554545545545455454554554545545455455454554554545545455455454554554545545455455454554545545545455455454554545545545455454554...

output:

450625621

result:

ok 1 number(s): "450625621"

Test #18:

score: 0
Accepted
time: 354ms
memory: 636084kb

input:

3000000
1181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181811811818118181181181811818118118181181181811818118118181181181811818118118181181811811818118118181181811811818118181181181811811818118181181181...

output:

649551870

result:

ok 1 number(s): "649551870"

Extra Test:

score: 0
Extra Test Passed