POPJ 2406 最小循环节 KMP裸题

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<math.h>
#include<algorithm>
#include<stdlib.h>
#include<queue>
#include<stack>
using namespace std;
#define N 1000010
char s[N];
int f[N];
void getFail(char *P){
	int i = 0, j = -1;
	f[0] = -1;
	while(P[i]){
		if(j == -1 || P[i] == P[j])
			f[++i] = ++j;
		else
			j = f[j];
	}
}
int main(){
	while(~scanf("%s",s),s[0] != ‘.‘){
		getFail(s);
		int len = strlen(s);
		int pos = len - f[len];
		int ans;
		if(len % pos)ans = 1;
		else ans = len /pos;
		printf("%d\n", ans);
	}	
	return 0;
}

POPJ 2406 最小循环节 KMP裸题

上一篇:解析Bellman-Ford算法求最短路径


下一篇:Python爬虫实践 -- 记录我的第二只爬虫