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, 发个改编算法导论的KMP纪念!

Posted by intheway at 2009-06-30 14:43:35 on Problem 3461
void Kmp(char *t, char *pat)
{
	pref[0] = -1;
	int k = -1, i;
	for(i = 1; pat[i] != 0; ++i)
	{
		while(k > -1 && pat[k+1] != pat[i])
			k = pref[k];
		if(pat[k+1] == pat[i])
			++k;
		pref[i] = k;
	}
	k = -1;
	for(i = 0; t[i] != 0; ++i)
	{
		while(k > -1 && pat[k+1] != t[i])
			k = pref[k];
		if(pat[k+1] == t[i])
			++k;
		if(pat[k+1] == 0)//k==strlen(pat)-1
		{
			//printf("matched at %d\n", i-k);
			++cnt;
			k = pref[k];
		}
	}
}

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