stackoverflow中找到了一个时间复杂度分析很棒的链接 https://www.inf.hs-flensburg.de/lang/algorithmen/pattern/kmpen.htm

判断字符串 str 中是否包含子串 match。

next [i] : match [i-1] 结尾的后缀子串(不包含match [0])和 match [0] 开头的前缀子串,两者的最大匹配长度。

  1. 因为match[0] 前面没有字符串,规定 next [0] == -1
  2. 因为 next [i] 对应的子串不包含 match [0],所以next[1] = 0

假设当前 str[i ... j-1] 和 match [0 ... j-i-1]:若 str [j] != match [j-i]

  1. 若next [j] != -1,下一个比较的位置不是 str [i+1] 和 match [0],而是 str[j] 和 match [next [j-i]]
  2. 若next [j] = -1,说明 match 的索引指向 match [0],即 j - i = 0,并且在上一次比较中,match [0] != str [j],此时 str 的索引加1即可。

算法的精髓在于搞清楚一个问题:str [i] 和 str [j] 之间是否存在以 str [j-1] 结尾且长度大于 next[j-i] 的子串呢?

答案显然是否定的,这违反了 next 数组的定义。

时间复杂度:O(N),分析:

先看匹配过程:

  1. 方法的循环体中有3个分支。
  2. 循环中,si++发生的次数等于 s.length - 1。因此,进入前2个分支的次数是 s.length - 1。
  3. 其次,mi回退(match滑动)的过程等价于 match 对应 str 往右至少一个位置,显然它往右(match滑动)的最大次数是 s.length - m.length。因此,进入最后1个分支的次数是s.length - m.length。
  4. 所以循环发生的次数 2 * s.length - m.length + 1,即2N-M+1。

再看next数组生成:

  1. 方法的循环体中有3个分支。
  2. 循环中,pos++发生的次数等于 m.length - 2。因此,进其中2个分支的次数是 m.length - 2。
  3. 其次,cn回退最多发生多少次,受限制于 ++cn 执行了多少次,++cn 和 pos++ 同时发生最多发生的次数是 m.length - 2。
  4. 所以循环发生的次数 2 * m.length - 4,即2M-4。

最后看总复杂度:

  1. (2N-M+1) + (2M-4) = (2N+M-3) = O(2N+M)
  2. 因为 N >= M,O(2N+M) = O(3N) = O(N)
public static int getIndexOf(String s, String m) {
if (s == null || m == null || m.length() < 1 || s.length() < m.length()) {
return -1;
}
char[] ss = s.toCharArray();
char[] ms = m.toCharArray();
int si = 0;
int mi = 0;
int[] next = getNextArray(ms);
while (si < ss.length && mi < ms.length) {
if (ss[si] == ms[mi]) {
//匹配
si++;
mi++;
} else if (next[mi] == -1) {
//当前mi = 0,str[si] != match[0],si++即可
si++;
} else {
//滑动
mi = next[mi];
}
}
return mi == ms.length ? si - mi : -1;
}

怎么计算next数组?

  1. match [0] == -1,match[1] = 0(原因已经给出)。
  2. 从左至右依次计算,计算 next [i] 时已知 next [0 ... i-1]
  3. 我们可以利用 next [i - 1],若 match [i-1] = match [next [i-1]],那么 next [i] = next[i-1] + 1(再长的话与next数组定义违背)
  4. 若 match [i-1] != match [next [i-1]],则比较 match[i-1] 和 match[next[next[i-1]]],原因如下:
    1. 假设next[i-1] 对应前后缀分别是A和B,那么 next [next [i-1]] 则代表A的前后缀最大匹配长度。
    2. 由于A=B,因此A的前缀能对应B的后缀。
    3. 当前可能性不可能大于 next [next [i-1]] + 1,否则与next数组定义违背。
  5. 第3步和第4步递归执行,直到 next [k] = 0,则令 next [i] = 0。
public static int[] getNextArray(char[] ms) {
if (ms.length == 1) {
return new int[] { -1 };
}
int[] next = new int[ms.length];
next[0] = -1;
next[1] = 0;
//当前将要计算的位置
int pos = 2;
//当前将要被比较的位置
int cn = 0;
while (pos < next.length) {
if (ms[pos - 1] == ms[cn]) {
// cn是位置,长度=位置+1
next[pos++] = ++cn;
//此刻,cn = next[pos - 1]
} else if (cn > 0) {
cn = next[cn];
} else {
next[pos++] = 0;
//此刻,cn = next[pos-1] = 0
}
}
return next;
}

KMP(超详细复杂度分析)的更多相关文章

  1. 利用 Docker Compose 搭建 SpringBoot 运行环境(超详细步骤和分析)

    0.前言 相信点进来看这篇文章的同学们已经对 Docker Dompose 有一定的了解了,下面,我们拿最简单的例子来介绍如何使用 Docker Compose 来管理项目. 本文例子: 一个应用服务 ...

  2. PHP yield 分析,以及协程的实现,超详细版(上)

    参考资料 http://www.laruence.com/2015/05/28/3038.html http://php.net/manual/zh/class.generator.php http: ...

  3. ArrayList源码分析超详细(转载)

    ArrayList源码分析超详细   ArrayList源码分析超详解 想要分析下源码是件好事,但是如何去进行分析呢?以我的例子来说,我进行源码分析的过程如下几步: 找到类:利用 IDEA 找到所需要 ...

  4. 超强、超详细Redis数据库入门教程

    这篇文章主要介绍了超强.超详细Redis入门教程,本文详细介绍了Redis数据库各个方面的知识,需要的朋友可以参考下 [本教程目录] 1.redis是什么2.redis的作者何许人也3.谁在使用red ...

  5. 超强、超详细Redis数据库入门教程(转载)

    这篇文章主要介绍了超强.超详细Redis入门教程,本文详细介绍了Redis数据库各个方面的知识,需要的朋友可以参考下   [本教程目录] 1.redis是什么 2.redis的作者何许人也 3.谁在使 ...

  6. 超强、超详细Redis入门教程【转】

    这篇文章主要介绍了超强.超详细Redis入门教程,本文详细介绍了Redis数据库各个方面的知识,需要的朋友可以参考下 [本教程目录] 1.redis是什么2.redis的作者何许人也3.谁在使用red ...

  7. 超详细Redis入门教程【转】

    这篇文章主要介绍了超强.超详细Redis入门教程,本文详细介绍了Redis数据库各个方面的知识,需要的朋友可以参考下   [本教程目录] 1.redis是什么 2.redis的作者何许人也 3.谁在使 ...

  8. c语言面试宝典(经典,超详细)

    c语言面试宝典(经典,超详细) 2018年08月25日 09:32:19 chengxuyuan997 阅读数:7799   摘自:https://blog.csdn.net/chengxuyuan9 ...

  9. SPSS超详细操作:分层回归(hierarchical multiple regression)

    SPSS超详细操作:分层回归(hierarchical multiple regression) 1.问题与数据 最大携氧能力(maximal aerobic capacity, VO2max)是评价 ...

随机推荐

  1. 折半搜索(meet in the middle)

    折半搜索(meet in the middle) ​ 我们经常会遇见一些暴力枚举的题目,但是由于时间复杂度太过庞大不得不放弃. ​ 由于子树分支是指数性增长,所以我们考虑将其折半优化; 前言 ​ 这个 ...

  2. spring再学习之设计模式

    今天我们来聊一聊,spring中常用到的设计模式,在spring中常用的设计模式达到九种. 第一种:简单工厂 三种工厂模式:https://blog.csdn.net/xiaoddt/article/ ...

  3. codeforces 1019B The hat 【交互题+二分搜索】

    题目链接:戳这里 学习题解:戳这里

  4. 容器之List接口下各实现类(Vector,ArrayList 和LinkedList)的线程安全问题

    Vector .ArrayList 和LinkedList都是List接口下的实现类,但是他们之间的区别和联系是什么呢? 首先: 然后: 如果您仅仅想知道结论,那么可以关闭了. 下面我讨论讨论为什么. ...

  5. js arrow function return object

    js arrow function return object bug filterData: { type: Object, default: () => {}, required: true ...

  6. hihoCoder Challenge 3

    #1065 : 全图传送 时间限制:30000ms 单点时限:3000ms 内存限制:256MB 描述 先知法里奥是 Dota 系列中的一个英雄.机动性强,推塔能力一流,打钱速度快,传送技能使先知可以 ...

  7. 23 种设计模式 APP & 23 Design Patterns App

    23 种设计模式 APP & 23 Design Patterns App https://github.com/xgqfrms/23-design-patterns-app https:// ...

  8. Apple CSS Animation All In One

    Apple CSS Animation All In One Apple Watch CSS Animation https://codepen.io/xgqfrms/pen/LYZaNMb See ...

  9. 使用 js 实现一个简易版的动画库

    使用 js 实现一个简易版的动画库 具有挑战性的前端面试题 animation css refs https://www.infoq.cn/article/0NUjpxGrqRX6Ss01BLLE x ...

  10. how to using js to realize notes feature on the website

    how to using js to realize notes feature on the website js & notes demos https://medium.com/brow ...