算法1:字符串模式匹配KMP算法 源代码
来源:互联网 发布:如何更换网络节点 编辑:程序博客网 时间:2024/06/06 05:54
#include <iostream>#include <cstring>using namespace std;void kmp_t(const char * s, int *t){ int len = strlen(s); if(len<=2){ t[0]=-1; t[1]=0; } else{ t[0]=-1; t[1]=0; int temp1,temp2; temp1 = 2; temp2 = 0; while(temp1<len){ if(s[temp1-1] == s[temp2]){ temp2++; t[temp1] = temp2; temp1++; } else if(temp2) temp2 = t[temp2]; else{ t[temp1] = 0; temp1++; } } } for(int i=0;i<len;i++) { cout<<s[i]<<" "<<t[i]<<endl; }}int kmp(const char * s1, const char *s2, int *t){ int len1 = strlen(s1); int len2 = strlen(s2); int m1=0, m2=0; while(m1+m2<len1){ cout<<m1<<" "<<m2<<endl; if(s1[m1+m2] == s2[m2]){ m2++; if(m2>=len2){return m1;} } else{ if(t[m2]==-1){ m1 += 1; m2 = 0; } else{ m1 += m2-t[m2]; m2 = t[m2]; } } } return -1;}int main(){ const char *s1; s1 = "ababctabfdabababcdab"; const char *s2; s2 = "ababcd"; int *t = new int[strlen(s2)]; int n_start; fill(t,t+strlen(s2),0); kmp_t(s2,t); n_start = kmp(s1,s2,t); cout<<n_start<<endl; for(int i=0; i<strlen(s1); i++) { if(i==n_start)cout<<" || "; if(i==n_start+strlen(s2))cout<<" || "; cout<<s1[i]<<" "; } return 0;}
0 0
- 算法1:字符串模式匹配KMP算法 源代码
- 字符串模式匹配KMP算法
- 字符串模式匹配KMP算法
- KMP字符串模式匹配算法
- 字符串模式匹配KMP算法
- KMP字符串模式匹配算法
- KMP字符串模式匹配算法
- 字符串模式匹配KMP算法
- KMP 字符串模式匹配算法
- 字符串模式匹配KMP算法
- 字符串模式匹配KMP算法
- 算法 字符串模式匹配KMP
- KMP字符串模式匹配算法
- 字符串KMP模式匹配算法
- KMP字符串模式匹配算法
- 字符串模式匹配----KMP算法
- kmp字符串模式匹配算法
- 字符串模式匹配KMP算法
- php实现直接插入排序
- 养成记录的习惯
- UVALive 6697
- VS2010/MFC编程入门之二十四(常用控件:列表框控件ListBox)
- jpeg压缩
- 算法1:字符串模式匹配KMP算法 源代码
- 数据结构实验之排序六:希尔排序
- UVALive 6807 Túnel de Rata (最大生成树)
- 并发编程之:Java内存模型
- linux 查看gcc版本,支持C++11
- 漫谈Java中的互斥同步
- HDMI 基础
- iOS 多页面滑动
- 安卓侧滑代码