KMP字符串匹配算法--算法导论示例zz
来源:互联网 发布:海康网络摄像机保存 编辑:程序博客网 时间:2024/05/01 05:19
The Knuth-Morris-Pratt string searching algorithm searches for occurrences of a "pattern" string within a main string by employing the simple observation that when a mismatch occurs, we have enough knowledge simply by possessing the pattern to determine where the next match could begin, thus bypassing re-examination of previously matched characters.
#include <iostream>
int *ComputePrefixFunction(char *p)
...{
int m=strlen(p);
int *c=new int[m];
memset(c,0,m*sizeof(int));
c[0]=0;
int k=0;
for(int q=2;q<=m;q++)
...{
while(k>0&&p[k]!=p[q-1])
k=c[k-1];
if(p[k]==p[q-1])
k=k+1;
c[q-1]=k;
}
return c;
}
void KMP_Matcher(char *t,char *p)
...{
int n=strlen(t);
int m=strlen(p);
int *c=new int[m];
memset(c,0,m*sizeof(int));
c=ComputePrefixFunction(p);
int q=0;
for(int i=1;i<=n;i++)
...{
while(q>0&&p[q]!=t[i-1])
q=c[q-1];
if(p[q]==t[i-1])
q=q+1;
if(q==m)
...{
printf("Pattern occurs with shift %d ",i-m+1);
q=c[q-1];
}
}
}
int main()
...{
KMP_Matcher("dsfdsfsshefgfdgshedsfdfshe","she");
return 0;
}
int *ComputePrefixFunction(char *p)
...{
int m=strlen(p);
int *c=new int[m];
memset(c,0,m*sizeof(int));
c[0]=0;
int k=0;
for(int q=2;q<=m;q++)
...{
while(k>0&&p[k]!=p[q-1])
k=c[k-1];
if(p[k]==p[q-1])
k=k+1;
c[q-1]=k;
}
return c;
}
void KMP_Matcher(char *t,char *p)
...{
int n=strlen(t);
int m=strlen(p);
int *c=new int[m];
memset(c,0,m*sizeof(int));
c=ComputePrefixFunction(p);
int q=0;
for(int i=1;i<=n;i++)
...{
while(q>0&&p[q]!=t[i-1])
q=c[q-1];
if(p[q]==t[i-1])
q=q+1;
if(q==m)
...{
printf("Pattern occurs with shift %d ",i-m+1);
q=c[q-1];
}
}
}
int main()
...{
KMP_Matcher("dsfdsfsshefgfdgshedsfdfshe","she");
return 0;
}
- KMP字符串匹配算法--算法导论示例zz
- 算法导论 KMP字符串匹配
- 算法导论KMP匹配字符串
- 算法导论-第32章- 字符串匹配 - KMP算法
- Java实现算法导论中KMP字符串匹配算法
- 【算法导论】字符串匹配
- 算法导论-字符串匹配
- KMP字符串匹配(按照《算法导论》伪代码实现)
- KMP 字符串匹配算法
- kmp字符串匹配算法
- kmp字符串匹配算法
- KMP字符串匹配算法
- 字符串匹配算法-kmp
- KMP(字符串匹配)算法
- KMP 字符串匹配算法
- 字符串匹配算法:KMP
- KMP算法 字符串匹配
- 字符串匹配 KMP 算法
- 中国房地产新政反思-转载
- ASP调用存储过程详解。
- 看了SAP的笔试题目,顺手写一个单件模式
- 数学的重要性
- 服了
- KMP字符串匹配算法--算法导论示例zz
- 2005八大必死网站----你的网站有这毛病么?
- 社会交往中的六大著名法则
- 使用分治法解决棋盘覆盖问题zz
- 学生信息管理系统
- 我该做什么
- 以前写过的动态sql例子
- symbian 利用蓝牙串口服务输出调试信息zz
- Google, Happy Birthday.