23.字符串匹配,KMP算法
来源:互联网 发布:淘宝运营网站 编辑:程序博客网 时间:2024/06/05 14:30
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<assert.h>//BF算法(一般思路的字符串匹配)int BF(const char *s, const char *sub, int pos) //pos表示从s的pos位置处开始匹配{int i = pos;int j = 0;int lens = strlen(s);int lensub = strlen(sub);while ((i < lens) && (j < lensub)){if (s[i] == sub[j]){i++;j++;}else{i = i - j + 1; //匹配失败,i回到开始匹配位置+1的下标处,j=0j = 0;}}if (j >= lensub){return i - j;}else{return -1;}}//KMP算法void GetNext(int *next, const char *sub){int lensub = strlen(sub);next[0] = -1;next[1] = 0;int i = 2;int k = 0;while (i < lensub){if ((k == -1) || (sub[i - 1] == sub[k])){//next[i] = k + 1;//i++;//k += 1;next[i++] = ++k;}else{k = next[k]; //不是k = next[i],因为是在k位置处出现失配}}//对next数组进行优化,当发生不匹配且next[j]元素与当前j下标元素相同,直接跳过,减少不必要的匹配for (int j = 1; j < lensub; j++){if (sub[next[j]] == sub[j]){next[j] = next[next[j]];}}}int KMP(const char *s, const char *sub, int pos){int i = pos;int j = 0;int lens = strlen(s);int lensub = strlen(sub);int *next = (int*)malloc(lensub * sizeof(int));assert(next != NULL);GetNext(next, sub); //得到sub的next数组while (i < lens && j < lensub){if ((j == -1) || (s[i] == sub[j])){i++;j++;}else{j = next[j];//匹配不相等时只改变j下标,i不用回退}}free(next);if (j >= lensub){return i - j;}else{return -1;}}int main(){char *s = "abcabababcabd";char *sub = "abcabd";//int pos = BF(s, sub, 0);int pos = KMP(s, sub, 0);printf("%d\n", pos);return 0;}
0 0
- 23.字符串匹配,KMP算法
- KMP 字符串匹配算法
- kmp字符串匹配算法
- kmp字符串匹配算法
- KMP字符串匹配算法
- 字符串匹配算法-kmp
- KMP(字符串匹配)算法
- KMP 字符串匹配算法
- 字符串匹配算法:KMP
- KMP算法 字符串匹配
- 字符串匹配 KMP 算法
- KMP字符串匹配算法
- KMP字符串匹配算法
- KMP算法:字符串匹配
- KMP字符串匹配算法
- 字符串匹配--KMP算法
- KMP字符串匹配算法
- KMP字符串匹配算法
- 怎么通过公众号来快速学习python编程?
- SylixOS SPI Flash驱动移植
- GYM 100827 I.Salary Inequity(线段树)
- Android LayoutInflater原理分析,带你一步步深入了解View(一)
- apt-get update和apt-get upgrade的区别
- 23.字符串匹配,KMP算法
- 八大种排序算法【java实现】
- centos apache2.4 设置 VirtualHost
- linux常用命令(持续更新)
- 撒大声地
- 你好
- 2.1数组的创建
- POJ1330:Nearest Common Ancestors(LCA + Tarjan离线处理)
- Innodb表压缩过程中遇到的坑(innodb_file_format)