bzoj 1355: Radio Transmission

题目大意:

求字符串的最短循环覆盖字符串

题解:

经典结论题: kmp

\(ans = n - next[n]\)

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
	x=0;char ch;bool flag = false;
	while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
	while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
const int maxn = 1000010;
int fail[maxn];
inline void kmp(char *s){
	int len = strlen(s);
	fail[0] = fail[1] = 0;
	for(int i=1,x;i<len;++i){
		x = fail[i];
		while(x != 0 && s[x] != s[i]) x = fail[x];
		fail[i+1] = s[x] == s[i] ? x + 1 : 0;
	}
}
char s[maxn];
int main(){
	int n;read(n);scanf("%s",s);
	kmp(s);
	printf("%d\n",n-fail[n]);
	getchar();getchar();
	return 0;
}
posted @ 2017-03-14 21:29  Sky_miner  阅读(202)  评论(0编辑  收藏  举报