标签:
自己YY一下可以发现answer = n - fail[ n ]
-----------------------------------------------------------------------------------
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#define rep( i , n ) for( int i = 0 ; i < n ; i++ )
#define clr( x , c ) memset( x , c , sizeof( x ) )
#define Rep( i , n ) for( int i = 1 ; i < n ; ++i )
using namespace std;
const int maxn = int( 1e6 ) + 5;
char S[ maxn ];
int fail[ maxn ];
int len;
void build_fail() {
fail[ 0 ] = fail[ 1 ] = 0;
Rep( i , len ) {
int t = fail[ i ];
while( t && S[ t ] != S[ i ] )
t = fail[ t ];
fail[ i + 1 ] = S[ t ] == S[ i ] ? t + 1 : 0;
}
}
void Read() {
cin >> len;
rep( i , len ) {
char c = getchar();
while ( ! islower( c ) ) c = getchar();
S[ i ] = c;
}
}
int main() {
// freopen( "test.in" , "r" , stdin );
Read();
build_fail();
cout << len - fail[ len ] << "\n";
return 0;
}
-----------------------------------------------------------------------------------
1355: [Baltic2009]Radio Transmission
Time Limit: 10 Sec Memory Limit: 64 MB
Submit: 419 Solved: 276
[Submit][Status][Discuss]Description
给你一个字符串,它是由某个字符串不断自我连接形成的。 但是这个字符串是不确定的,现在只想知道它的最短长度是多少.
Input
第一行给出字符串的长度,1 < L ≤ 1,000,000. 第二行给出一个字符串,全由小写字母组成.
Output
输出最短的长度
Sample Input
8
cabcabca
Sample Output
3
HINT
对于样例,我们可以利用"abc"不断自我连接得到"abcabcabc",读入的cabcabca,是它的子串
Source
BZOJ 1355: [Baltic2009]Radio Transmission( kmp )
标签:
原文地址:http://www.cnblogs.com/JSZX11556/p/4558794.html