KMP字符串匹配
来源:互联网 发布:金融数据挖掘 pdf 编辑:程序博客网 时间:2024/05/23 19:53
#include<iostream>#include<cstdio>#include<cstring>using namespace std;void creatNext(char* str,int* next,int len){ next[0]=0; for(int i=1;i<len;i++) { if(str[next[i-1]]==str[i]) { next[i]=next[i-1]+1; } else { next[i]=0; } }}int kmp(char* str,char* sub_str){ int i=0; int str_Len=strlen(str); int substr_Len=strlen(sub_str); int sunstrStartIndex=0; int next[substr_Len]; creatNext(sub_str,next,substr_Len); while(1) { while(sunstrStartIndex<substr_Len&&i<str_Len&&sub_str[sunstrStartIndex]==str[i]) { sunstrStartIndex++; i++; } if(sunstrStartIndex==substr_Len) { return i-substr_Len; } else if(i==str_Len) { return -1; } else { if(sunstrStartIndex==0) i++; else { sunstrStartIndex=next[sunstrStartIndex-1]; } } }}int main(){ char* str="BBC ABCDAB ABCDABCDABDE"; char* sub_str="ABCDABD";// char* str="aaac";// char* sub_str="aaaaaaa"; int result=kmp(str,sub_str); cout<<result<<endl;}
0 0
- KMP 字符串匹配算法
- kmp字符串匹配算法
- kmp字符串匹配算法
- KMP字符串匹配算法
- 字符串匹配算法-kmp
- KMP(字符串匹配)算法
- 字符串匹配 KMP
- KMP 字符串匹配算法
- 字符串匹配算法:KMP
- KMP算法 字符串匹配
- 字符串匹配 KMP 算法
- KMP字符串匹配(1)
- KMP字符串匹配(2)
- KMP字符串匹配(3)
- KMP字符串匹配算法
- KMP字符串匹配
- KMP字符串匹配算法
- KMP字符串匹配
- Sqlserver数据库通过jdbc查询日期结果比实际少两天
- poj 1000 A+B Problem
- UML使用经验及总结
- 网络之 TCP封包、粘包、半包
- boost常用库
- KMP字符串匹配
- 正则表达式简结
- AngularJS之指令
- 聊聊Java源码 — HashMap(1)
- 【XML】使用DOM解析XML文件
- Android简单布局优化1
- Python 之 sklearn 实现 PCA 降维
- android微信支付之调起支付和支付结果页面回调
- Html之文件上传