Surface
Portal
Ideas
First turn the face to adult speech:
For each prefix I of a given string, the longest, so that the string can be repeated on both sides of the original prefix I prefix (that is, a prefix of prefix i), all of these "prefix prefix" length and
Take advantage of the nature of $next$: prefix $i$ has a length of $next[i]$ prefix and suffix is equal
This shows: If I have a public prefix length of J, then the prefixes I have a period of i-j
See
Obviously the blue line in the picture is a period of black segment
So the next question is easy:
First find out the $next$ array
For each prefix $i$, make $j=i$, and then in $j>0$ case ordered $j=next[j]$, the smallest $j$ is the answer, at this time $ans+=i-j$
An optimization: After finding out $j$, make $j=fail[i]$, this can speed up the recursion speed (equivalent to memory)
Code
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define LL Long Longusing namespaceStdChara[1000010];intn,fail[1000010];intMain () {scanf ("%d", &n); scanf ("%s", a);intI,j;ll cnt=0; fail[0]=fail[1]=0; j=0; for(i=1; i<n;i++) {//Solve next while(j&& (A[i]!=a[j])) j=fail[j]; j+= (A[i]==a[j]); fail[i+1]=j; } for(i=1; i<=n;i++) {j=i; while(Fail[j]) j=fail[j];if(fail[i]!=0) Fail[i]=j;//MemoryCnt+=i-j; } printf ("%lld", CNT);}
[POI2006] [luogu3435] Okr-periods of Words [Kmp+next array]