POJ 2406 kmp

//10305331	c00h00g	2406	Accepted	4984K	125MS	C++	485B	2012-06-08 12:04:58
#include<iostream>
using namespace std;
char t[1000005];
int next[1000005];

void getnext(char *t,int len){
	int i=1,j=0;
	next[1]=0;
	while(i<=len){
		if(j==0||t[i]==t[j]){
			i++,j++,next[i]=j;
		}
		else
			j=next[j];
	}
}

int main(){
	while(scanf("%s",t+1)){
		if(t[1]=='.')
			break;

		int len=strlen(t+1);
		getnext(t,len);
		int length=len-next[len+1]+1;
		if(len%length==0)
			printf("%d\n",len/length);
		else
			printf("1\n");
	}
	return 0;
}

你可能感兴趣的:(c)