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

AC

Posted by OZY123 at 2016-03-29 13:52:58 on Problem 3461
#include<cstdio>
#include<cstring>
const int N=1000005;
char s[N];
char k[N];
int f[N];
int len,len1;
int ans=0;
void make ()
{
	f[0]=-1;
	int i=1,j=-1;
	for (;i<len;i++)
	{
		while (j!=-1&&s[i]!=s[j+1]) j=f[j];
		if (s[i]==s[j+1]) j++;
		f[i]=j;
	}
	return ;
}
void get_ans()
{
	ans=0;
	int i=0,j=-1;
	for (;i<len1;i++)
	{
		while (j!=-1&&k[i]!=s[j+1]) j=f[j];
		if (k[i]==s[j+1]) j++;
		if (j==len-1)
		{
			ans++;
			j=f[j];
		}
	}
	return ;
}
int main()
{
	int T;
	scanf("%d",&T);
	while (T--)
	{
		scanf("%s%s",s,k);
		len=strlen(s);
		len1=strlen(k);
		make();
		get_ans();
		printf("%d\n",ans);
	}
	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