KMP算法
来源:互联网 发布:sass for mac中文版 编辑:程序博客网 时间:2024/06/05 08:53
//KMP#include<iostream>#include<cstdio>#include<cstring>#include<string.h>void get_next(char *T,int *next){next[1] = 0;int j = 1;int i = 0;while(j < T[0])//T[0]存放字符串的长度,j为后缀,i为前缀{if(0 == i||T[i] == T[j]){i++;j++;if(T[i]!=T[j]){next[j] = i;}else{ next[j] = next[i];}}else{i = next[i]; //回溯 }// 前缀是固定的,后缀的是相对的}}//返回子串T在主串S第pos个字符后的位置,若不存在则返回0int Index_KMP(char *S,char *T){ int i = 1;int j = 1;int next[255];get_next(T,next);while(i<=S[0]&&j<=T[0]){ if(0 == j||S[i]==T[j]){ i++;j++;}else{ j = next[j];//用模式匹配中next[j]位置去匹配}}if(j>T[0]){ return i-T[0];}else{ return 0;}}int main(){ char ss[255] = " abcdeasdwa";char tt[255] = " abcd";int sl = strlen(ss)-1;int tl = strlen(tt)-1; ss[0] = sl;tt[0] = tl;int flag = 0; int pos = Index_KMP(ss,tt); if(pos!=0) { printf("能够匹配\n位置为%d\n",pos); flag = 1; }if(!flag){ printf("不匹配\n");}system("PAUSE");}
0 0
- KMP算法详解 【KMP】
- 【KMP】KMP算法模板
- KMP hihoCoder1015 KMP算法
- kmp算法
- KMP算法
- KMP算法
- KMP算法
- KMP算法
- KMP 算法
- kmp算法
- KMP算法
- kmp算法
- KMP算法
- KMP算法
- kmp算法
- kmp算法
- KMP算法
- KMP算法
- poj-1207-The 3n + 1 problem
- YK线上机器cpu、内存信息
- ZOJ 3946 Highway Project (spfa)
- http通信
- HTML基础知识总结
- KMP算法
- mySQL
- IOS在后台每隔一段时间执行一下 资料2
- 时间复杂度与空间复杂度
- bzoj 1176: [Balkan2007]Mokia 【CDQ分治】
- hdoj-5590-ZYB's Biology
- Java 代码块
- 【mybatis基础】mybatis开发dao两种方法
- Oracle 11g 管理Oracle 集群