[POI2006] [luogu3435] Okr-periods of Words [Kmp+next array]

Source: Internet
Author: User

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]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.