| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
为何我老是TLE#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator