Luogu 3435 POI2006OKR-Periods of Words(kmp)

Posted gloid

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Luogu 3435 POI2006OKR-Periods of Words(kmp)相关的知识,希望对你有一定的参考价值。

  显然答案应该是Σi-next[next[……next[i]]] (next[next[……next[i]]]>0)。递推即可。

#include<iostream> 
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
int read()
{
    int x=0,f=1;char c=getchar();
    while (c<0||c>9) {if (c==-) f=-1;c=getchar();}
    while (c>=0&&c<=9) x=(x<<1)+(x<<3)+(c^48),c=getchar();
    return x*f;
}
#define N 1000010
int n,nxt[N],f[N];
long long ans=0;
char s[N];
int main()
{
#ifndef ONLINE_JUDGE
    freopen("a.in","r",stdin);
    freopen("a.out","w",stdout);
    const char LL[]="%I64d
";
#else
    const char LL[]="%lld
";
#endif
    n=read();scanf("%s",s+1);
    nxt[0]=-1;
    for (int i=1;i<=n;i++)
    {
        int j=nxt[i-1];
        while (~j&&s[j+1]!=s[i]) j=nxt[j];
        nxt[i]=j+1;f[i]=nxt[i]==0?i:f[nxt[i]];
    }
    for (int i=1;i<=n;i++) ans+=i-f[i];
    cout<<ans;
    return 0;
}

 

以上是关于Luogu 3435 POI2006OKR-Periods of Words(kmp)的主要内容,如果未能解决你的问题,请参考以下文章

[POI2006][luogu3435] OKR-Periods of Words [kmp+next数组]

P3435 [POI2006]OKR-Periods of Words

P3435 [POI2006]OKR-Periods of Words

P3435 [POI2006]OKR-Periods of Words

[POI2006]OKR-Periods of Words

[POI2006]ORK-Ploughing