3441. 唐纳德与子串 (Easy)

jizhuozhi

为什么暴力方法能过去而用KMP的统计算法过不去

void getNext(const std::string&P,int*next){
int i=0,j=next[0]=-1;
int len=P.length();
while(i<len){
while(j==-1||P[i]==P[j])next[++i]=++j;
j=next[j];
}
}

int KMP(const std::string&T,const std::string&P,Long*next){
int i=0,j=0;
int l1=T.length(),l2=P.length();
getNext(P, next);
int count=0;
while(i<l1&&j<l2){
if(j==-1||T[i]==P[j])++i,++j;
else j=next[j];
if(j==l2){count++;–i;j=next[j-1];}
}return count;
}

神秘的bboy

…可能你写得不好,我是暴力KMP过的

你当前正在回复 博客/题目
存在问题!