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

照书打了个标准的。。。

Posted by carber at 2010-04-20 20:35:28 on Problem 3461
int *COMPUTE_PREFIX_FUNCTION(char *P)
{
	int m = strlen(P);
	int *pai = new int[m+1];
	pai[0] = -1;
	int k = -1;
	for(int q = 1; q < m; q++)
	{
		while(k>-1&&P[k+1]!=P[q])
			k = pai[k];
		if(P[k+1]==P[q])
			k++;
		pai[q] = k;
	}
	return pai;
}
int KMP_MATCHER(char *T, char *P)
{
	int n = strlen(T);
	int m = strlen(P);
	int *pai = COMPUTE_PREFIX_FUNCTION(P);
	int q = -1, count = 0;
	for(int i = 0; i < n; i++)
	{
		while(q>-1&&P[q+1]!=T[i]) 
			q = pai[q];
		if(P[q+1]==T[i])
			q++;
		if(q == m-1)
		{
			count++;
			q = pai[q];
		}
	}
	delete[] pai;
	return count;
}

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