| ||||||||||
| 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<stdio.h>
#include<string.h>
#define MAXN 1000005
char s[MAXN];
char t[10005];
int next[MAXN];
int cnt;
void GetNext(){ // 计算next[]的值.
int i,j;
int t_len;
t_len=strlen(t);
i= 0;
j= -1;
next[0]=-1;
while (i< t_len - 1){
if (j== -1 || t[j]==t[i]){
++i;
++j;
if (t[i]!= t[j]){
next[i]= j;
}
else {
next[i]=next[j];
}
}
else {
j= next[j];
}
}
}
void KMPIndex(){ // KMP算法.
int i, j;
int s_len,t_len;
s_len=strlen(s);
t_len=strlen(t);
i=0;j=0;
while(i<s_len){
if(j == -1||s[i]==t[j]){
++i;
++j;
}
else{
j=next[j];
}
if (j>=t_len){
cnt++;
j=0;
if(i<s_len)i=i-t_len+1;
}
}
}
int main(){
int n;
scanf("%d",&n);
getchar();
while(n--){
cnt=0;
scanf("%s%s",t,s);
GetNext();
KMPIndex();
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