KMP算法详解
来源:互联网 发布:九阴绝学暗器数据 编辑:程序博客网 时间:2024/06/05 06:33
哇。。KMP学了后就一两个月没碰过。。结果今天看就不会了~~~~这里是我看到的最好的对KMP算法的解析。
KMP算法详解
代码模板:
#include<stdio.h>#include<string.h>void makeNext(const char P[],int next[]){ int q,k; int m = strlen(P); next[0] = 0; for (q = 1,k = 0; q < m; ++q) { while(k > 0 && P[q] != P[k]) k = next[k-1]; if (P[q] == P[k]) { k++; } next[q] = k; }}int kmp(const char T[],const char P[],int next[]){ int n,m; int i,q; n = strlen(T); m = strlen(P); makeNext(P,next); for (i = 0,q = 0; i < n; ++i) { while(q > 0 && P[q] != T[i]) q = next[q-1]; if (P[q] == T[i]) { q++; } if (q == m) { printf("Pattern occurs with shift:%d\n",(i-m+1)); } } }int main(){ int i; int next[20]={0}; char T[] = "ababxbababcadfdsss"; char P[] = "abcdabd"; printf("%s\n",T); printf("%s\n",P ); // makeNext(P,next); kmp(T,P,next); for (i = 0; i < strlen(P); ++i) { printf("%d ",next[i]); } printf("\n"); return 0;}
阅读全文
0 0
- KMP算法详解 【KMP】
- KMP算法详解
- KMP算法详解
- KMP算法详解
- KMP算法详解
- KMP算法详解
- KMP算法详解 转帖
- KMP算法详解
- KMP算法详解
- KMP算法详解
- KMP算法详解
- KMP算法详解 转帖
- KMP匹配算法详解
- KMP算法详解
- KMP算法详解
- KMP算法详解
- 转:KMP算法详解
- KMP算法详解(转)
- Java集合-HashSet(一)
- Linux安装SVN服务器及配置步骤
- Spark开发调优
- Orcle多行数据变一行
- 使用html+css实现一个个人照片墙
- KMP算法详解
- 5-引用的本质分析
- 1037. 在霍格沃茨找零钱(20)
- .NET 内存监测(Ants Memory Profiler)
- mysql 字段操作
- lattice 的Synplify pro综合工具,以及RTL寄存器查看
- 5.5.54升级5.6.83后,降级遇到的主库复制信息找不到问题
- 解决MySQL中写入中文出现乱码的问题
- 解析Guitar Pro 7如何设置局部速度