求助,蒟蒻六个点TLE

P3435 [POI2006] OKR-Periods of Words

_stOrz_ @ 2021-07-05 16:36:35

#include<bits/stdc++.h>
using namespace std;

char s[1000005];
int n,next[1000005];

int main(void)
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin >> n >> s;
    long long sum = 0,j = 0;
    next[0] = next[1] = 0;
    for(int i = 1;i < n; i++)
    {
        while(j and s[i] != s[j]) j = next[j];
        if(s[i] == s[j]) j++;
        next[i + 1] = j;
    }
    for(int i = 1;i <= n; i++)
    {
        j = i;
        while(next[j]) j = next[j];
        if(next[j]) next[i] = j;
        sum += i - j;
    }
    cout << sum << "\n";
}

by Lsz_2024111368 @ 2021-10-05 01:25:51

你好!

if(next[j]) next[i] = j;

应改为

if(next[i]) next[i] = j;

|