hdu1358 Period KMP中next数组的变形

http://acm.hdu.edu.cn/showproblem.php?pid=1358

转载:http://www.cnblogs.com/jackge/archive/2013/01/05/2846006.html

#include <bits/stdc++.h>
using namespace std;
const int maxn=1000005;
char s[maxn];
int nxt[maxn],n;
void get_nxt(){
	int i=0;
	int j=nxt[0]=-1;
	while(i<=n){
		if(j==-1||s[i]==s[j]){
			i++; j++;
			nxt[i]=j;
		}
		else{
			j=nxt[j];
		}
	}
}
int main(){
	int cnt=0;
	while(scanf("%d",&n)!=EOF){
		memset(s,0,sizeof(s));
		memset(nxt,0,sizeof(nxt));
		if(!n) break;
		scanf("%s",s);
		get_nxt();
		printf("Test case #%d\n",++cnt);
		for(int i=1;i<=n;i++){
			if(nxt[i]==0) continue;
			else{
				if(i%(i-nxt[i])==0){
					printf("%d %d\n",i,i/(i-nxt[i]));
				}
			}
		}
		printf("\n");
	}
	return 0;
}

 

没有更多推荐了,返回首页