next:

/*!
* Description:
* author scictor <scictor@gmail.com>
* date 2018/7/4
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// https://tekmarathon.com/2013/05/14/algorithm-to-find-substring-in-a-string-kmp-algorithm/
/*What is Partial Match Table?
It is an array of size (pattern_length+1) where, for each position of i in pattern p, b[i] is defined such that it takes the ‘length of the longest proper suffix of P[1…i-1]’ that matches with the ‘prefix of P’.
What is longest prefix/suffix match??? Proper prefix is a prefix which is not same as the substring. Recall proper set which is a subset of a set but is not same as the set.
Why a prefix should match suffix of the pattern? its because when we shift the pattern its the prefix of P which comes towards the suffix. And also the key idea is that if we have successfully matched prefix P[1…i-1] of the pattern with the substring T[j-(i-1)…j-1] of the input string and P(i)!=T(j), then we dont need to reprocess any of the suffix T[j-(i-1)…j-1] since we know this portion of the text string is the prefix of the pattern that we have just matched.
*/
//"ababacb"
/**
* Pre processes the pattern array based on proper prefixes and proper
* suffixes at every position of the array
*
* @param ptrn
* word that is to be searched in the search string
* @return partial match table which indicates
*/
void kmp_next(const char *pattern, int patternLen, int *next) {
int i = , j = -; next[i] = j; // default next[0] = -1
while (i < patternLen) {
while (j >= && pattern[i] != pattern[j]) {
// if there is mismatch consider the next widest border
// The borders to be examined are obtained in decreasing order from
// the values b[i], b[b[i]] etc.
j = next[j];
}
i++;
j++;
next[i] = j;
}
for(int index = ; index < patternLen; ++index) printf("%d ", next[index]);
return;
} /**
* Based on the pre processed array, search for the pattern in the text
*
* @param text
* text over which search happens
* @param ptrn
* pattern that is to be searched
*/ //int matchIndex[128] = {0}; int kmp_search(const char *text, int textLen, const char *pattern, int patternLen) {
int i = , j = ; // initialize new array and preprocess the pattern
int next[patternLen + ];
memset(next, 0x00, sizeof(next)); // int idx = 0;
// memset(matchIndex, 0x00, sizeof(matchIndex)); kmp_next(pattern, patternLen, next); while (i < textLen) {
while (j >= && text[i] != pattern[j]) {
j = next[j];
}
i++;
j++; // a match is found
// if (j == patternLen) {
// printf("found substring at index:" + (i - patternLen));
// j = next[j];
// }
if (j == patternLen) {
printf("found substring at index:%d", (i - patternLen));
//j = next[j];
//matchIndex[idx++] = i - patternLen;
return (i - patternLen);
}
} // for(int k = 0; k < idx; ++k)
// {
// printf("%d ", matchIndex[k]);
// }
return -;
} /*
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
Text(T) a b c a b d a b c a b d a b c a b d a b d a b c
Pattern(P) a b c a b d a b c
PMT (next[i]) -1 0 0 0 1 2 0 1 2 3
*/
// 4ms
int kmp(const char *text, int textLen, const char *pattern, int patternLen)
{
int *T;
int i, j; if (pattern[] == '\0')
return ; // Construct the lookup table
T = (int*) malloc( (patternLen + ) * sizeof(int) );
T[] = -;
for (i=; pattern[i] != '\0'; i++) {
T[i+] = T[i] + ;
while (T[i+] > && pattern[i] != pattern[T[i+]-])
T[i+] = T[T[i+]-] + ;
} for(int k = ; k < patternLen; ++k) printf("%d ", T[k]); // Perform the search
for (i=j=; text[i] != '\0'; ) {
if (j < || text[i] == pattern[j]) {
++i, ++j;
if (pattern[j] == '\0') {
return (i-j);
}
}
else j = T[j];
} free(T);
return -;
} /*const char *kmp_search(const char *text, const char *pattern)
{
int *T;
int i, j;
const char *result = NULL; if (pattern[0] == '\0')
return text; // Construct the lookup table
T = (int*) malloc((strlen(pattern)+1) * sizeof(int) );
T[0] = -1;
for (i=0; pattern[i] != '\0'; i++) {
T[i+1] = T[i] + 1;
while (T[i+1] > 0 && pattern[i] != pattern[T[i+1]-1])
T[i+1] = T[T[i+1]-1] + 1;
} // Perform the search
for (i=j=0; text[i] != '\0'; ) {
if (j < 0 || text[i] == pattern[j]) {
++i, ++j;
if (pattern[j] == '\0') {
result = text+i-j;
break;
}
}
else j = T[j];
} free(T);
return result;
}
*/

nextval:

void preKmp(char *x, int m, int kmpNext[]) {
int i, j; i = ;
j = kmpNext[] = -;
while (i < m) {
while (j > - && x[i] != x[j])
j = kmpNext[j];
i++;
j++;
if (x[i] == x[j])
kmpNext[i] = kmpNext[j];
else
kmpNext[i] = j;
}
} // text:y, len: n, match parttern:x, len: m
int KMP(char *y, int n, char *x, int m) {
int i, j, kmpNext[m]; /* Preprocessing */
preKmp(x, m, kmpNext); /* Searching */
i = j = ;
while (j < n) {
while (i > - && x[i] != y[j])
i = kmpNext[i];
i++;
j++;
// if (i >= m) {
// printf("j-i=%d\n",j - i);
// i = kmpNext[i];
// }
if (i == m) {
//printf("j-i=%d\n",j - i);
return j - i;
//i = kmpNext[i];
}
}
return -;
} int strStr(char* haystack, char* needle) {
int needleLen = strlen(needle);
if(needleLen == ) return ;
int hayLen = strlen(haystack);
if(hayLen == ) return -; return KMP(haystack, hayLen, needle, needleLen);
}

C语言实现KMP模式匹配算法的更多相关文章

  1. [从今天开始修炼数据结构]串、KMP模式匹配算法

    [从今天开始修炼数据结构]基本概念 [从今天开始修炼数据结构]线性表及其实现以及实现有Itertor的ArrayList和LinkedList [从今天开始修炼数据结构]栈.斐波那契数列.逆波兰四则运 ...

  2. KMP模式匹配算法

    KMP模式匹配算法 相信很多人对于这个还有点不了解,或者说是不懂,下面,通过一道题,来解决软考中的这个问题! 正题: aaabaaa,其next函数值为多少? 对于这个问题,我们应该怎么做呢? 1.整 ...

  3. 线性表-串:KMP模式匹配算法

    一.简单模式匹配算法(略,逐字符比较即可) 二.KMP模式匹配算法 next数组:j为字符序号,从1开始. (1)当j=1时,next=0: (2)当存在前缀=后缀情况,next=相同字符数+1: ( ...

  4. C++编程练习(7)----“KMP模式匹配算法“字符串匹配

    子串在主串中的定位操作通常称做串的模式匹配. KMP模式匹配算法实现: /* Index_KMP.h头文件 */ #include<string> #include<sstream& ...

  5. 详细解读KMP模式匹配算法

    转载请注明出处:http://blog.csdn.net/fightlei/article/details/52712461 首先我们需要了解什么是模式匹配? 子串定位运算又称为模式匹配(Patter ...

  6. 字符串的模式匹配算法——KMP模式匹配算法

    朴素的模式匹配算法(C++) 朴素的模式匹配算法,暴力,容易理解 #include<iostream> using namespace std; int main() { string m ...

  7. 串、KMP模式匹配算法

    串是由0个或者多个字符组成的有限序列,又名叫字符串. 串的比较: 串的比较是通过组成串的字符之间的编码来进行的,而字符的编码指的是字符在对应字符集中的序号. 计算机中常用的ASCII编码,由8位二进制 ...

  8. 浅谈KMP模式匹配算法

    普通的模式匹配算法(BF算法) 子串的定位操作通常称为模式匹配算法 假设有一个需求,需要我们从串"a b a b c a b c a c b a b"中,寻找内容为"a ...

  9. 数据结构(三)串---KMP模式匹配算法

    (一)定义 由于BF模式匹配算法的低效(有太多不必要的回溯和匹配),于是某三个前辈发表了一个模式匹配算法,可以大大避免重复遍历的情况,称之为克努特-莫里斯-普拉特算法,简称KMP算法 (二)KMP算法 ...

随机推荐

  1. python基础学习笔记(十一)

    迭代器 本节进行迭代器的讨论.只讨论一个特殊方法---- __iter__  ,这个方法是迭代器规则的基础. 迭代器规则 迭代的意思是重复做一些事很多次---就像在循环中做的那样.__iter__ 方 ...

  2. Uniform Generator HDU1014

    题意 给你公式seed(x+1) = [seed(x) + STEP] % MOD ,输入step和mod, 问你是否可以从第一项0,算到mod,它们是否都不同 是 good choice 否则 ba ...

  3. pair work结对编程(张艺 杨伊)

    一.结对编程人员: 张艺(学号后三位:185) 杨伊(学号后三位:151) 二.这是我们工作的样子:(图片) 三.结对编程优缺点:  优点:  1.结对编程时间紧密,在一定程度上可以督促双方学习,提高 ...

  4. 【个人总结】软件工程M1/M2总结

    个人博客连接: http://www.cnblogs.com/lwq12061168/p/4094252.html http://www.cnblogs.com/lwq12061168/p/40284 ...

  5. M2项目测试

    更为详细的测试报告,我们会在后续整理出来. 在M1的基础上,我们新增加了两个个数据表来存放问答对以及标签信息的表:C705question表 与 tag表 第二次迭代中,我们积极地同第三组沟通,了解到 ...

  6. 跟踪分析Linux内核的启动过程--实验报告 分析 及知识重点

    跟踪分析Linux内核的启动过程 攥写人:杨光  学号:20135233 ( *原创作品转载请注明出处*) ( 学习课程:<Linux内核分析>MOOC课程http://mooc.stud ...

  7. Junit4使用实验报告

    一.题目简介 Junit4的使用及求和测试. 二.源码的github链接 https://github.com/bjing123/test-/blob/master/Arithmetic.txt ht ...

  8. 20150409作业3 阅读《构建之法》1-5章 (Update:2015-04-16

    以下是我看<构建之法>1-5章列出来的知识点和一些自己对部分知识的理解以及一些吐槽...和感受 1.1 软件 = 程序 + 软件工程 (软件工程 = 软件 - 程序(我知道软件是什么,也知 ...

  9. <四则运算>第二次冲刺

    这一次冲刺的主要内容是完善我们的界面,是我们的APP界面更规划更标准一点, 然后还要添加一些新算法. 距离客户的需求已经一半了. 代码正在完善中,稍后上传...

  10. jstack 使用一例

    31jstack -m -F 2340 >libra.log 2>&1 jstack -m -F 2340 >libra2.log 2&>1 jstack -m ...