KMP算法
1.目的
在主串中快速,快速,快速地找到目标串




2.求解next数组


void getNext(StrNonfix substr,int next[]){
int j=1,t=0;
next[1]=0;
while(j<substr.length){
if(t==0||substr.ch[j] == substr.ch[t]){
next[j+1]=t+1;
++t;
++j;
}else{
t=next[t];
}
}
}3.KMP算法
int KMP(StrNonfix str,StrNonfix substr,int next[]){
int i=1,j=1;
while(i<=str.length && j<=substr.length){
if(j==0||str.ch[i] == substr.ch[j]){
++i;
++j;
}else{
j=next[j];
}
}
//模式串在主串中的初始位置
if(j>substr.length){
return i-substr.length;
}else{
return 0;
}
}4.改进
对KMP算法的改进主要体现在对Next数组的改进上


这里假设Pd,Pc,Pb都与Pj相同,Pa与Pj不同。

1.当Pk不等于Pj时,nextval[k]=next[k]=j;
2.当PK等于Pj时候,nextval[k]=nextval[next[k]]=nextval[j];


void getNextval(StrNonfix substr,int nextval[]){
int j=1,t=0;
nextval[1]=0;
while(j<substr.length){
if(t==0||substr.ch[j] == substr.ch[t]){
if(substr.ch[j+1] !=substr.ch[t+1]){
nextval[j+1]=t+1;
}else{
nextval[j+1]=nextval[t+1];
}
++t;
++j;
}else{
t=nextval[t];
}
}
} 相关推荐
troysps 2020-05-30
Happyunlimited 2020-05-01
RememberMePlease 2020-04-30
shawsun 2020-03-23
yedaoxiaodi 2020-01-25
wulaxiaohei 2019-12-29
蜗牛慢爬的李成广 2020-01-11
yedaoxiaodi 2020-01-02
rein0 2019-12-14
darlingtangli 2019-11-17
seekerhit 2019-11-05
Happyunlimited 2019-11-04
ustbfym 2019-11-03
蜗牛慢爬的李成广 2019-11-03
HTML学堂码匠 2019-04-03
KDF000 2013-02-14