[BOI2009] Radio Transmission radio 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 and output formats

Input formats:

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

The second line gives a string, all lowercase letters.

Output formats:

Output of the shortest length

Sample input and output

Input Sample # 1: 
8 
cabcabca
Output Sample # 1: 
3

Explanation

For the sample, we can use the "abc" continuous self-ligated to obtain "abcabcabc", read cabcabca, is that it's a substring

 

analysis:

This problem is the basis of the title of KMP (also simpler than template ???), this question is seeking minimum cycle length of the string s, which we call "loop substring s" and the conclusion is: ans = n- next [n].

 

CODE:

 

 1 #include<cstdio>
 2 #include<iostream>
 3 using namespace std;
 4 const int maxn=1000005;
 5 int n,next[maxn];
 6 char s[maxn];
 7 int main(){
 8     scanf("%d%s",&n,s+1);
 9     int j=0;
10     for(int i=2;i<=n;++i){
11         if(j&&s[i]!=s[j+1]) j=next[j];
12         if(s[i]==s[j+1]) ++j;
13         next[i]=j;
14     }
15     printf("%d",n-next[n]);
16     return 0;
17 }

 

Guess you like

Origin www.cnblogs.com/kanchuang/p/11137625.html