/*
kmp彻底理解
next 数组 :用来指导S【i】串 T【j】串 对应字符失配
指导 i 不回溯,即j应该走多少个位置
next[j]:j位置前一个元素 需要
计算某个字符对应的next值,就是看这个j对应字符之前的字符串中有多大长度的相同前缀后缀 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void get_Next(char *pattern,int next[])
{
/*
j前缀 i后缀
*/
int j = -1;
int i = 0;
int m = strlen(pattern);
next[0] = -1; while(i < m - 1)
{
//pattern[j]表示前缀,pattern[i]表示后缀
if(j == -1||pattern[j] == pattern[i])
{
i++;
j++;
if(pattern[j] != pattern[i])
{
next[i] = j;
}
else
{
next[i] = next[j];
} }
else
{
//一旦失配 前缀 回到上次匹配的位置
j = next[j];
} }
} int KmpSearch(char* s, char* p)
{
int i = 0;
int j = 0;
int sLen = strlen(s);
int pLen = strlen(p);
int next[10]; get_Next(p,next);
while (i < sLen && j < pLen)
{
//①如果j = -1,或者当前字符匹配成功(即S[i] == P[j]),都令i++,j++
if (j == -1 || s[i] == p[j])
{
i++;
j++;
}
else
{
//②如果j != -1,且当前字符匹配失败(即S[i] != P[j]),则令 i 不变,j = next[j]
//next[j]即为j所对应的next值
j = next[j];
}
}
if (j == pLen)
return i - j;
else
return -1;
} int main(void)
{
char text[] = "ABC ABCDAB ABCDABCDABDE";
char pattern[] = "ABCDABD";
char *ch = text;
int i = KmpSearch(text, pattern); if(i >= 0) printf("matched@: %s\n", ch + i);
printf("pos is %d\n",i); return 0;
}

kmp代码实现的更多相关文章

  1. KMP 代码 暂存

    #include <stdio.h> #include <string.h> ],B[]; ]; int n, m; void _next(){ ; ; next[] = ; ...

  2. KMP算法以及优化(代码分析以及求解next数组和nextval数组)

    KMP算法以及优化(代码分析以及求解next数组和nextval数组) 来了,数据结构及算法的内容来了,这才是我们的专攻,前面写的都是开胃小菜,本篇文章,侧重考研408方向,所以保证了你只要看懂了,题 ...

  3. 从头到尾彻底理解KMP

    从头到尾彻底理解KMP 作者:July 时间:最初写于2011年12月,2014年7月21日晚10点 全部删除重写成此文,随后的半个多月不断反复改进. 1. 引言 本KMP原文最初写于2年多前的201 ...

  4. KMP详解

    原文: http://blog.csdn.net/v_july_v/article/details/7041827 从头到尾彻底理解KMP 1. 引言 本KMP原文最初写于2年多前的2011年12月, ...

  5. POJ 1961 Period( KMP )*

    Period Time Limit: 3000MSMemory Limit: 30000K Total Submissions: 12089Accepted: 5656 Description For ...

  6. KMP算法详解 --从july那学的

    KMP代码: int KmpSearch(char* s, char* p) { ; ; int sLen = strlen(s); int pLen = strlen(p); while (i &l ...

  7. POJ3461–Oulipo(KMP)

    题目大意 给定一个文本串和模式串,求模式串在文本串中出现的次数 题解 正宗KMP 代码: #include<iostream> #include<cstring> #inclu ...

  8. 【转】从头到尾彻底理解KMP

    很好,讲得很清晰,值得学习. 作者:July时间:最初写于2011年12月,2014年7月21日晚10点 全部删除重写成此文,随后的半个月从早到晚不断改进. 1. 引言 本KMP原文最初写于2年多前的 ...

  9. Problem 2128 最长子串(kmp+strstr好题经典)

     Problem 2128 最长子串 Accept: 134    Submit: 523Time Limit: 3000 mSec    Memory Limit : 65536 KB  Probl ...

随机推荐

  1. C#进程与线程

    public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { / ...

  2. hdu5392 Infoplane in Tina Town(LCM)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Infoplane in Tina Town Time Limit: 14000/ ...

  3. [Leetcode][019] Remove Nth Node From End of List (Java)

    题目在这里: https://leetcode.com/problems/remove-nth-node-from-end-of-list/ [标签] Linked List; Two Pointer ...

  4. 利用css进行网页布局

    网页布局: 又称版式布局,是网页UI设计师将有限的视觉元素进行有机的排列组合,将理性的思维个性的化的表现出来,是一种具有个人艺术特色的视觉传达方式.传达信息的同时有美感.网页设计特点(相对纸媒来说). ...

  5. php 之 类,对象(三)多态性,函数重载,克隆

    一.三大特性之三 多态性(在php中表象不明显)1.概念:当父类引用指向子类实例时,由于子类对父类函数进行了重写,导致我们在使用该引用去调用相应的方法显示出的不同.2.发生条件:1.必须有继承 2. ...

  6. ADT-位图

    利用位图数据结构实现排序,利用每一位的下标作为索引,每位的值作为属性值,可以表示存在或不存在,适合存储稠密的数据,排序遍历的范围会是索引的最大值 最后发现耗时比python中list自带的sort多, ...

  7. Json数据转换

    using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...

  8. IOS 解析歌词lrc

    最近在捣鼓音乐播放器,过程中学到了一些东西,写下来分享一下,首先是歌词的解析 首先我们看看lrc(不贴维基了怕打不开 歌词文件一般是这样的格式 1.[分钟:秒.毫秒] 歌词 2. [分钟:秒] 歌词 ...

  9. Pick two points at random from the interior of a unit square, what is the expected distance between them?

    My solution is as folllowing. This integration is hard to solve. I googled it, and found the result ...

  10. 【转】ubuntu14.04 trusty的源

    原文网址:http://blog.chinaunix.net/uid-15041-id-4821715.html 一.编辑更新源文件:/etc/apt/sources.list二.更新源索引文件:ap ...