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

为何我老是TLE

Posted by 200841193 at 2010-07-11 23:03:04 on Problem 3461
#include <iostream>
using namespace std;
int m, n;

char t[100000];
char s[1000000];
int next[1000000];

void get_next(char *t, int *next)
{
	int i = 0;
	next[0] = -1;
	int j = -1;
	while(i < strlen(t))
	{
		if(j == -1 || t[i] == t[j])
		{
			++i;
			++j;
			if(t[i] != t[j])
				next[i] = j;
			else
				next[i] = next[j];
		}
		else
			j = next[j];
	}
}


int Index_KMP(char *s, char *t)
{
	int sum = 0;
	int i = 0, j = 0;
	int m = strlen(s), n = strlen(t);
	while(i < m)
	{
		if(j == -1 || s[i] == t[j])
		{
			++i;
			++j;
		}
		else
			j = next[j];
		if(j >= n)
		{
			//printf("i = %d\n", i-n);
			sum++;
			j = 0;
			i = i-n+1;
		}
	}
	return sum;
}

int main()
{
	int nCases;
	scanf("%d", &nCases);
	while(nCases--)
	{
		getchar();
		scanf("%s", t);
		getchar();
		scanf("%s", s);
		get_next(t, next);
		int cnt = Index_KMP(s, t);
		printf("%d\n", cnt);
	}
	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