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 sailors at 2007-11-08 17:50:07 on Problem 3461
In Reply To:kmp为什么不过? Posted by:sailors at 2007-11-08 17:46:56
#include<iostream>
#include<cstring>
using namespace std;
#define N 1000100
struct String
{
	char s[N];
	int len;
};
String S, T;
int next[N];
int getnext()
{
	int i,j;
    next[1]=0;
    j=0;
    for(i=2;i<=T.len;i++)
    {
        while(j>0&&T.s[j+1]!=T.s[j])j=next[j];
        if(T.s[j+1]==T.s[i])j=j+1;
        next[i]=j;
    }
	return 0;
}
int kmp()
{
 	int i,j;
	j=0;
	int cn=0;
	for(i=1;i<=S.len;i++)
	{
		while(j>0&&T.s[j+1]!=S.s[i])j=next[j];
 		if(T.s[j+1]==S.s[i])j=j+1;
		if(j==T.len)
		{
			//printf("%dggg  \n",i-T.len);
			cn++;
			j=next[j];
		}	
	}
	return cn;
}
char ss[N],tt[N];
int main()
{
	int i;
	int ca;
	int len1;
	//freopen("kmp.in","r",stdin);
	//freopen("kmp.out","w",stdout);
	scanf("%d",&ca);
	{
		while(ca--)
		{
			scanf("%s%s",tt,ss);
			len1=strlen(ss);
			S.len=len1;
			for(i=0;i<len1;i++)
			{
				S.s[i+1]=ss[i];
			}
			len1=strlen(tt);
			T.len=len1;
			for(i=0;i<len1;i++)
			{
				T.s[i+1]=tt[i];
			}
			getnext();
			int res=kmp();
			printf("%d\n",res);
		}
	}
	
	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