AmazingCounters.com

BZOJ 1355[Baltic2009]Radio Transmission

1355: [Baltic2009]Radio Transmission

Time Limit: 10 Sec  Memory Limit: 64 MB

Description

给你一个字符串,它是由某个字符串不断自我连接形成的。 但是这个字符串是不确定的,现在只想知道它的最短长度是多少.

Input

第一行给出字符串的长度,1 < L ≤ 1,000,000. 第二行给出一个字符串,全由小写字母组成.

Output

输出最短的长度

Sample Input

8
cabcabca

Sample Output

3

HINT

对于样例,我们可以利用"abc"不断自我连接得到"abcabcabc",读入的cabcabca,是它的子串

 

需要想几分钟的kmp。。答案显然是n-next[n]

 1 #include<set>
 2 #include<queue>
 3 #include<cstdio>
 4 #include<cstdlib>
 5 #include<cstring>
 6 #include<iostream>
 7 #include<algorithm>
 8 using namespace std;
 9 const int N = 1000010;
10 #define For(i,n) for(int i=1;i<=n;i++)
11 #define Rep(i,l,r) for(int i=l;i<=r;i++)
12 char st[N];
13 int n,next[N];
14 
15 void BuildNext(){
16     next[0]=next[1]=0;
17     For(i,n){
18         int j=next[i];
19         while(j&&st[i]!=st[j]) j=next[j];
20         if(st[i]==st[j]) next[i+1]=j+1;
21         else             next[i+1]=0;
22     }
23 }
24 
25 int main(){
26     scanf("%d",&n);
27     scanf("%s",&st);
28     BuildNext();
29     printf("%d\n",n-next[n]);
30     return 0;
31 }

 

posted @ 2014-09-05 19:34  ZJDx1998  阅读(221)  评论(0编辑  收藏  举报