kmp算法--求字符串子串--《数据结构》严蔚敏
来源:互联网 发布:弹珠枪在淘宝上怎么搜 编辑:程序博客网 时间:2024/06/06 00:46
// exam1.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <iostream>using namespace std;void get_next(int* &next,char* s){int j=0;int i=1;int len=strlen(s);next=(int*)malloc(len*sizeof(int));memset(next,0,len*sizeof(int));while(s[i]!='\0'){if(s[j]==s[i]){i++;j++;if(s[i]!=s[j]){next[i]=j;}else{next[i]=next[j];}}else{if(j==0){i++;}j=next[j];}}}int kmp(int* next,char* s,char* m){int i=0,j=0;while(s[i]!='\0'){if(m[j]=='\0'){break;}if(s[i]==m[j]){i++;j++;}else{if(j==0){i++;}j=next[j];}}return i-strlen(m);}int main(void){int len,*next;char *s="acabaabaabcacaabc";char *m="abaabcac";len=strlen(m);get_next(next,m);int loc=kmp(next,s,m);/*for(int i=0;i<len;i++){cout<<next[i]<<endl;}*/cout<<loc<<endl;system("pause");return 0;}
code2
#include "stdafx.h"#include <iostream>#include <string>#include <stack>using namespace std;void getNext(char* s,int* &next){<span style="white-space:pre"></span>next=new int[strlen(s)];<span style="white-space:pre"></span>memset(next,0,strlen(s)*sizeof(int));<span style="white-space:pre"></span>int i=0;<span style="white-space:pre"></span>int j=-1;<span style="white-space:pre"></span>next[0]=-1;<span style="white-space:pre"></span>while(i<strlen(s))<span style="white-space:pre"></span>{<span style="white-space:pre"></span>if(j==-1 || s[i]==s[j])<span style="white-space:pre"></span>{<span style="white-space:pre"></span>i++;<span style="white-space:pre"></span>j++;<span style="white-space:pre"></span>if(s[i]==s[j])<span style="white-space:pre"></span>{<span style="white-space:pre"></span>next[i]=next[j];<span style="white-space:pre"></span>}<span style="white-space:pre"></span>else<span style="white-space:pre"></span>{<span style="white-space:pre"></span>next[i]=j;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}<span style="white-space:pre"></span>else<span style="white-space:pre"></span>{<span style="white-space:pre"></span>j=next[j];<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}}void kmp(char* s,char* t,int* next){<span style="white-space:pre"></span>int i=0;<span style="white-space:pre"></span>int j=0;<span style="white-space:pre"></span><span style="white-space:pre"></span>int len1=strlen(s);<span style="white-space:pre"></span>int len2=strlen(t);<span style="white-space:pre"></span>while(i<len1 && j<len2)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>if(j==-1 || s[i]==t[j])<span style="white-space:pre"></span>{<span style="white-space:pre"></span>i++;<span style="white-space:pre"></span>j++;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>else<span style="white-space:pre"></span>{<span style="white-space:pre"></span>j=next[j];<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}<span style="white-space:pre"></span>if(j==strlen(t))<span style="white-space:pre"></span>{<span style="white-space:pre"></span>cout<<s+i-strlen(t)<<endl;<span style="white-space:pre"></span>}}int main(void){<span style="white-space:pre"></span>char* s="acabaabaabcacaabc";<span style="white-space:pre"></span>char* t="abaabcac";<span style="white-space:pre"></span>int* next;<span style="white-space:pre"></span>getNext(t,next);<span style="white-space:pre"></span>kmp(s,t,next);<span style="white-space:pre"></span>system("pause");<span style="white-space:pre"></span>return 0;}
0 0
- kmp算法--求字符串子串--《数据结构》严蔚敏
- 利用KMP算法,求顺序表存储的两个字符串的最长公共子串
- KMP算法字符串查找子串
- KMP子字符串查找算法
- KMP算法:查找子字符串
- 数据结构 字符串比较 KMP算法
- 【kmp算法】求一个串的重复子串
- KMP算法求公共回文子串的个数
- 【数据结构与算法】字符串匹配KMP算法
- 透彻理解KMP算法 - 字符串匹配/子串查找
- 查找子字符串----KMP算法深入剖析
- 查找子字符串----KMP算法深入剖析
- java实现子字符串的KMP算法
- KMP子字符串查找算法.java
- HDU1358 字符串求周期 KMP算法
- 【jzoj4910】【子串】【字符串】【kmp】
- C++数据结构之字符串的KMP算法
- 数据结构 字符串模式匹配之KMP算法
- Python3.3 print readline 乱码
- POJ 3259--Wormholes(求负环)
- Jackson解析首字母大写的json字符串
- Android开发图形处理创建一个图形的拷贝
- XML笔记
- kmp算法--求字符串子串--《数据结构》严蔚敏
- 网络拓扑距离的高效KNN查询 (中期总结)
- CountDownLatch使用说明
- HDU 4906 Our happy ending 状压DP
- Python3.3 Djando1.6.5 整合 MySQL Connectort/Python
- 51Talk-Level 7 Unit 1 L2
- 利用resteasy框架构建rest webservice----第六波:实现文件下载
- 用C语言宏定义寄存器地址解释 和 关键字 volatile
- SEO