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

C++福利

Posted by 1908847416 at 2016-04-07 13:32:27 on Problem 3461
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;

int n,la,lb;
int next[100010];
char a[1000010],b[100010];

void getnext()
{
	int j=-1;
	next[0]=-1;
	for(int i=1;i<lb;i++)
	{
		if(j!=-1 && b[j+1]!=b[i])	j=next[j];
		if(b[j+1]==b[i])	j++;
		next[i]=j;
	}
}

int kmp()
{
	int ans=0;
	int j=-1;
	for(int i=0;i<la;i++)
	{
		if(j!=-1 && a[i]!=b[j+1])	j=next[j];
		if(b[j+1]==a[i])	j++;
		if(j==lb-1)	
		{
			ans++;	
			j=next[j];
		}	
	}
	return ans;
}

int main()
{
	scanf("%d",&n);
	for(int u=1;u<=n;u++)
	{
		scanf("%s %s",b,a);
		la=strlen(a);
		lb=strlen(b);
		getnext();
		printf("%d\n",kmp());
	}
}

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