KMP模式匹配算法
来源:互联网 发布:什么是数据api接口 编辑:程序博客网 时间:2024/06/10 06:13
首先,这是由Knuth、Morris和Prattle三人设计的线性时间字符串匹配算法。
这里就不仔细的讲解,网上有更好的讲解,在这推荐几个自己当时学习的KMP所看到的较好理解的
- http://www.cnblogs.com/c-cloud/p/3224788.html
- http://www.61mon.com/index.php/archives/183/
这里附上自己所学的代码:
#include<bits/stdc++.h> using namespace std;/*s[]:是主串p[]:是模式串*/int next[10000];void getNext(char *s){ int i,j; i = 0; j = next[0] = -1; int s_len = strlen(s); while(i<s_len){ while(j!=-1&&s[i]!=s[j]){ j = next[j]; } next[++i] = ++j; }}int KMP(char *s,char *p){ int s_len = strlen(s); int p_len = strlen(p); int i,j,ans; getNext(p); i = ans = j =0; while(i<s_len){ while(j!=-1&&p[j]!=s[i]){ j = next[j]; } i++,j++; if(j>=p_len){ ans++; j = next[j]; printf("%d\n",i-1); //输出p[]所在的位置 } } return ans; //返回p[]在s[]中出现的次数 }int main(){ char s[] = "abababab"; char p[] = "aba"; printf("%d\n",KMP(s,p)); return 0;}
阅读全文
0 0
- 模式匹配---KMP算法
- 模式匹配 KMP算法
- 模式匹配-KMP算法
- KMP模式匹配算法
- KMP模式匹配算法
- KMP模式匹配算法
- 模式匹配kmp算法
- 模式匹配算法kmp
- KMP模式匹配算法
- KMP模式匹配算法
- KMP模式匹配算法
- KMP模式匹配算法
- KMP模式匹配算法
- KMP模式匹配算法
- KMP模式匹配算法
- KMP模式匹配算法
- KMP算法模式匹配
- KMP 模式匹配算法
- Android View的layout_width属性是如何解析的
- POJ
- JavaScript中最大值最小值问题
- 上下界网络流
- 世界是不公平的,唯有感受是私有
- KMP模式匹配算法
- C#使用Xamarin开发可移植移动应用(5.进阶篇显示弹出窗口与通讯中心)附源码
- Java/C++实现快速排序
- ubuntu16.0源码编译opencv3.3.0
- 63 Unique Paths II
- spingDataJpa
- 转载: HDMI 基础知识
- MonggoDB In Action-更新、原子操作与删除(Part3)
- shell小脚本--laod博客更新hosts文件[脚本]