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

Re:我用KMP过的代码

Posted by micweaver at 2010-05-02 21:04:59 on Problem 2406
In Reply To:郁闷啊,忘了把C换成C++,两个CE,有那个同学好心把KMP做的发给我么,只在算法导论上看过不会用.. Posted by:lijinwu at 2010-01-20 19:30:39
#include <stdio.h>
#include <string.h>

char s[1000010];
int next[1000010];

int len;
int main()
{

    int i,j;
	while(1)
	{
		scanf("%s",s);
		if(s[0]=='.')break;
        len=strlen(s);
		i=0;next[0]=-1;j=-1;
		while(i<len)
		{
			if(j==-1||s[i]==s[j])
			{
				++i;
				++j;
				next[i]=j;
			}else j=next[j];
		}
		if(len%(len-next[len])==0)
		{
			printf("%d\n",len/(len-next[len]));
		}else printf("1\n");
		 
	}

	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