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 zjnu09220233 at 2011-01-29 19:36:01 on Problem 3461
#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:
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