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 10:56:12 on Problem 2406 and last updated at 2014-06-12 10:56:27
贴个代码

//题意:就是问一个字符串写成(a)^n的形式,求最大的n.
//已知字符串长度为l,对应的next[l],则回溯区间长d=k-next[k],如果k%d==0,那么t[1……k]最多可均匀的分成k/d份,
//也就是可以生成一个长度为d的重复度为k/d的字串。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char s[1000005];
int next[1000005];
int l;
void getnext()
{
	int j=0,k=-1;
	next[0]=-1;
	while(j<l)
	{
		if(k==-1 || s[j]==s[k])
		{
			j++;k++;
			if(s[j]!=s[k])
				next[j]=k;
			else
				next[j]=next[k];
		}
		else k=next[k];
	}
}
int main()
{
	int ans;
	while(scanf("%s",s) && strcmp(s,"."))
	{
		ans=1;
		l=strlen(s);
		getnext();
		if(l%(l-next[l])==0)
			ans=l/(l-next[l]);
		cout<<ans<<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