Wireless transmission

Title Description
give you a string, it is by a string connected to form a continuous self. But the string is uncertain, and now just want to know how much its shortest length.

Input format
The first line gives the length of the string, 1 <L ≤ 1,000,000.

The second line gives a string, all lowercase letters.

Output format of
the output of the shortest length

Sample Input Output
Input # 1 replication
. 8
cabcabca
output # 1 Copy
3
Description / prompt
for the sample, we can use "abc" continuous self-ligated to obtain "abcabcabc", read cabcabca, it substring

This question is seeking minimum cycle length string ss, which we call "ss cycle substring"

Introducing conclusion:

ans=n-next[n]

Find the maximum common prefixes and suffixes of the string;

Program is very short, but the concentrated essence of all (after all, KMP)

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

#define Q 1000005

char a[Q];

int p[Q];

int s,i,j;
int main()
{
    scanf("%d",&s);
    scanf("%s",a+1);
    for(i=2;i<=s;i++){
        while(j&&a[j+1]!=a[i]){
            j=p[j];
        }
        if(a[j+1]==a[i]){
            j++;
        }
        p[i]=j; 
    }
    printf("%d",s-p[s]);
}

Guess you like

Origin blog.csdn.net/sericon/article/details/94741920