KMP算法详解
来源:互联网 发布:拒绝退款 淘宝介入 编辑:程序博客网 时间:2024/06/10 11:14
关于Next数组,我已经有过说明
但是有了next数组怎么用呢
现在我进行简单的图解,为大家讲解一下
简单设置两个变量I,q;
#include <iostream>using namespace std;void GetKMP(char* target,int target_length,int next[]);void KMP(const char* P,const char* T,int next[]);void main(){ char* s="abcabcabcabcabcabcabc"; char* p="abcabc"; int *next=new int[strlen(p)]; GetKMP(p,strlen(p),next); KMP(p,s,next);}void GetKMP(char* target,int target_length,int next[]){ next[0]=0; int j; for(int i=1;i<target_length;i++) { j=next[i-1]; if(target[j]==target[i]) { next[i]=next[i-1]+1; } else { next[i]=0; } }}void KMP(const char* P,const char* T,int next[]){int m=strlen(P);//匹配字符串的长度int n=strlen(T);//目标字符串的长度int q=0;//匹配字符串索引GetKMP((char*)P,m,next);int i=0;while(i<n && q<m){ if(T[i]!=P[q]) { if(q>0) { q=next[q-1]; } else { i++; } } if(T[i]==P[q]) { i++; q++; } if(q==m-1) { cout<<"匹配的位置为:"<<i-m+1<<endl; i++; q=next[q]; }}}
阅读全文
0 0
- KMP算法详解 【KMP】
- KMP算法详解
- KMP算法详解
- KMP算法详解
- KMP算法详解
- KMP算法详解
- KMP算法详解 转帖
- KMP算法详解
- KMP算法详解
- KMP算法详解
- KMP算法详解
- KMP算法详解 转帖
- KMP匹配算法详解
- KMP算法详解
- KMP算法详解
- KMP算法详解
- 转:KMP算法详解
- KMP算法详解(转)
- ORA-39358: Export Dump File Version 12.2.0 Not Compatible With Target Version 11.2.0
- play1.x 第一弹 :与play的初次邂逅(搭建,目录结构,生命周期)
- LightOJ 1018 Brush (IV)(状态压缩DP)
- 基本算法--递推
- 如何让 Xcode 在读写上提速100倍?
- KMP算法详解
- 20170814周末记账流水(2017-08-14 10:00)
- Maven快速入门
- emWin 2天速成实例教程005_控件聚焦和用户消息的使用方法
- 初识Android图片加载之Glide
- Luogu P2679 [NOIp提高组2015]子串
- jquery_01
- 最大流DFS(EK)算法模板
- 数据结构上机实验之二分查找之平衡二叉树