Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

KMP 贴个代码

Posted by 452181625 at 2014-06-12 12:11:56 on Problem 1961
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char s[1000005];
int next[1000005];
int n;
void getnext()
{
	int j=0,k=-1;
	next[0]=-1;
	while(j<n)
	{
		if(k==-1 || s[j]==s[k])//求距离不修正。
		{
			j++;k++;
			next[j]=k;
		}
		else k=next[k];
	}
}
int main()
{
	int i,t=1;
	while(scanf("%d",&n) && n)
	{
		scanf("%s",s);
		getnext();
		cout<<"Test case #"<<t++<<endl;
		for(i=2;i<=n;i++)
		{
			int t=i%(i-next[i]);
			int k=i/(i-next[i]);
			if(t==0 && k>1)
				cout<<i<<' '<<k<<endl;
		}
		cout<<endl;
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator