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

234

Posted by YPZXW at 2016-11-12 20:12:02 on Problem 3461
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char S[1000006],T[1000006];
int mjs[1000006];
int lens,lent;
int kmp(int mjs[]){
	int i,j;
	int ans=0;
	j=0;
	for(i=0;i<lens;i++){
		while(j&&S[i]!=T[j]) j=mjs[j]; 
		if(S[i]==T[j]) j++;
		if(j>=lent)
		   ans++,j=mjs[j];
	}
	return ans;
}
void work(int lenx,int wxz[]) {
	wxz[1]=0;
	for(int i=2;i<=lenx;i++) {
		int j=wxz[i-1];
		while(j&&T[j]!=T[i-1]) j=wxz[j];
		if(T[j]==T[i-1]) j++;
		wxz[i]=j;
	}
}
int main()
{
	int t;
	scanf("%d",&t);
	for(int ss=1;ss<=t;ss++){
	scanf("%s%s",T,S);
	lens=strlen(S);
	lent=strlen(T);
	work(lent,mjs);
	int love_mjs=kmp(mjs);
	printf("%d\n",love_mjs);
	}
	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