String Similarity 为啥米KMP会超时呢,不是死循环,我测过100000的,只过了4/10 test case int test_num; char str[100024]; int F[100024]; long long ans; void FailureFunction(char P[], int F[],int m){ int i,j; F[0]=0; // assignment is important! j=0; i=1; while(iif(P[i]==P[j]){ F[i]=j+1; i++; j++; }else if(j>0){ j=F[j-1]; }else { F[i]=0; i++; } } } void solve(int m) { int i=m; int life=-1; while (i--) { if (F[i]<0) { life=-F[i]; ans-=(long long)F[i]; } else { int j=i; while (F[j]>0) { j=F[j]; if (life-1 == j || F[j]<0) { /////// life=abs(F[j]); /////// ans+=(long long)abs(F[j]); break; } } } } } void KMP(char P[]){ int m=strlen(P); FailureFunction(P,F,m); int i=m; while (i--) { if (m-1 == i) { F[i]*=-1; } else { if (abs(F[i+1]) != F[i]+1) { F[i]*=-1; } } } ans=m; solve(m); std::cout<} int main() { std::cin>>test_num; std::cin.getline(str,200); while (test_num--) { std::cin.getline(str,100024); KMP(str); } }
b*t
37 楼
100000个a的时候?
【在 b*****c 的大作中提到】 : String Similarity : 为啥米KMP会超时呢,不是死循环,我测过100000的,只过了4/10 test case : int test_num; : char str[100024]; : int F[100024]; : long long ans; : void FailureFunction(char P[], int F[],int m){ : int i,j; : F[0]=0; // assignment is important! : j=0;