2019年06月17日
阅读 585
评论 0
喜欢 0
- 前缀常识:Sq→Sequence,指顺序结构, Li→Link,指链接结构
- malloc的用法:struct SqString p=(SqString )malloc(sizeof(SqString));
顺序串的存储结构
#define MaxSize 100
struct SqString {
string data;
int length;
};
KMP算法
void GetNext(SqString t,int next[]) {
int j,k;
j=0;
k=-1;
next[0]=-1;
while(j<t.length-1) {
if(k==-1 || t.data[j]==t.data[k]) {
j++;
k++;
next[j]=k;
} else k=next[k];
}
}
int KMPIndex(SqString s,SqString t) {
int next[MaxSize],i=0,j=0;
GetNext(t,next);
while(i<s.length && j<t.length) {
if(j==-1 || s.data[i]==t.data[j]) {
i++;
j++;
} else j=next[j];
}
if(j>=t.length)
return i-t.length;
else
return -1;
}
暂无评论