KMP算法
来源:互联网 发布:spss软件如何下载 编辑:程序博客网 时间:2024/06/07 07:15
KMP算法
#include<stdio.h>#include<stdlib.h>#include<string.h>int n,m;int next[1010];char s[10100],t[101];void ne(char s[]){ for(int i=1;i<=m;i++){ int j=next[i-1]; while(j>0 && s[j+1]!=s[i])j=next[j]; if(t[j+1]==t[i])next[i]=j+1; else next[i]=0; }}// next数组用来存储当前字符的前缀的最长相等前缀后缀的长度,不能重叠void work(char s[],char t[]){ int l=0,i,j,k,n,m; for(i=0;i<strlen(s);i++){ if(s[i]!=t[l]){ l=0;i+=next[i];continue;//不断跳到当前点的next数组上 } l++; if(l==strlen(t)){printf("%d\n",i-l+2);return;}//到达目标就退出 }}int main(){ int i,j,k; gets(s);n=strlen(s); gets(t);m=strlen(t); work(s,t);//主工作函数 return 0;}
以上就是kmp代码的具体实现,kmp能在O(n+m)(n是母串的长度,m是字串的长度)的时间复杂度里实现字符串匹配。
1 1
- KMP算法详解 【KMP】
- 【KMP】KMP算法模板
- KMP hihoCoder1015 KMP算法
- kmp算法
- KMP算法
- KMP算法
- KMP算法
- KMP算法
- KMP 算法
- kmp算法
- KMP算法
- kmp算法
- KMP算法
- KMP算法
- kmp算法
- kmp算法
- KMP算法
- KMP算法
- 程序员的观察力
- mysql 通过bin-log恢复数据方法详解
- Linux 文件权限管理 之 RWX
- 利用JAXBContext将xml文件转换成List<T>
- 浅谈Java反射(1)
- KMP算法
- Spring从入门到精通视频教程合集
- hdoj 1010 Tempter of the Bone (dfs 奇偶剪枝)
- Lucene学习笔记之(五)lucene的特殊查询
- Linux 文件权限管理 之 ACL
- 190. Reverse Bits
- Python基础算法/剑指offer
- Linux 文件权限管理 之 特殊权限
- 缓存相关