The function used here is from the leetcode. Details can be found in leetcode problem: Implement strStr()

The best explanation should be made in the comments, which can be understood by the leading of code.

// next[j]: the smallest valid position we need to check next when detect mismatch at jth character pattern[j]
// Here, valid position means "pattern[0, ..., next[j]-1]" are matched with "text". void getNext(char *pattern, int next[]){
int i = 0, j = -1; // If the "text[i]" fails to match "patter[0]", then we need to
// check "text[i+1]" and "patter[0]", which also means "text[i]"
// would check with "patter[-1]".
next[i] = j; // while loop 1:
while(pattern[i] != '\0){
// while loop 2:
while(j >= 0 && pattern[i] != pattern[j]){
// First, j need to be valid index, so it needs to be not less than 0.
// Then, if "pattern[i]" fails to match "pattern[j]", we can also think as
// "text[i]" fails to match "pattern[j]".
// So we need to check if "text[i]" matches with "pattern[next[j]]", as next[j]
// is the position we need to check when we fail at position j.
j = next[j];
} // After the above while loop, we can know that "text[0, ..., i]" matches
// "pattern[0, ..., j]", so we can move one more step for both "text" and "pattern".
++i; ++j; // For the new i, marked as i_new, we can determine its "next value" now!!
// As we've known that "text[0, ..., i_new - 1]" matches "pattern[0, ..., j_new - 1]",
// if we fail to match at position "text[i_new]", we can move pattern to the j_new position to
// check if "text[i_new]" matches "pattern[j_new]".
// P.S:
// Also, we can know the j_new position is the optimized position. If we can get a valid position j' (valid
// means "pattern[0, ..., j'-1]" are matched with "text") smaller
// than j_new, then we'd get "(j' - 1)" (which is valid at position j'-1) is smaller than "next[j]",
// which is contradicted to the definition of "next" table.
if(pattern[i] == pattern[j])
next[i] = next[j];
else
next[i] = j;
}
} char *strStr(char *text, char *pattern){
if(NULL == text || NULL == pattern)
return NULL;
if('\0' == pattern[0])
return text; // i is the pointer of text, j is the pointer of pattern.
int i = 0, j = 0;
char *pos = NULL;
int *next = new int[strlen(pattern) + 1]; // include the '\0' getNext(pattern, next); while(text[i] != '\0'){
// Same optimization in getNext(), that is
// if we fail at one position, we may also fail at the
// next position, which means we can continue along the "next" table
// Also, we need the index to be valid first.
while(j >= 0 && text[i] != pattern[j])
j = next[j]; // After the while loop, we can know "text[0, ..., i]" matches "pattern[0, ..., j]"
// So we need to move one more step for both "text" and "pattern".
++i; ++j; if(pattern[j] == '\0'){
pos = (text + i) - j; // The beginning position in text which corresponding to the matched pattern position.
return pos;
}
} return pos; }

KMP string pattern matching的更多相关文章

  1. Symbols of String Pattern Matching

    Symbols of String Pattern Matching in Introduction to Algorithms. As it's important to be clear when ...

  2. Beginning Scala study note(5) Pattern Matching

    The basic functional cornerstones of Scala: immutable data types, passing of functions as parameters ...

  3. scala pattern matching

    scala语言的一大重要特性之一就是模式匹配.在我看来,这个怎么看都很像java语言中的switch语句,但是,这个仅仅只是像(因为有case关键字),他们毕竟是不同的东西,switch在java中, ...

  4. [PureScript] Break up Expressions into Cases in PureScript using Simple Pattern Matching

    Pattern matching in functional programming languages is a way to break up expressions into individua ...

  5. [Scala] Pattern Matching(模式匹配)

    Scala中的match, 比起以往使用的switch-case有著更強大的功能, 1. 傳統方法 def toYesOrNo(choice: Int): String = choice match ...

  6. pattern matching is C# 7.0

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/is 原来的版本 private static s ...

  7. C#9.0 终于来了,带你一起解读Pattern matching 和 nint 两大新特性玩法

    一:背景 1. 讲故事 上一篇跟大家聊到了Target-typed new 和 Lambda discard parameters,看博客园和公号里的阅读量都达到了新高,甚是欣慰,不管大家对新特性是多 ...

  8. Zhu-Takaoka Two-dimensional Pattern Matching

    Two dimensional pattern matching. Details may be added later.... Corresponding more work can be foun ...

  9. TypeError: cannot use a string pattern on a bytes-like object的解决办法

    #!/usr/python3 import re import urllib.request def gethtml(url): page=urllib.request.urlopen(url) ht ...

随机推荐

  1. leetcode79

    class Solution { public boolean exist(char[][] board, String word) { for(int i=0; i<board.length; ...

  2. 悲观锁,乐观锁,排他锁,行锁----MYSQL

    在说具体的锁结构时,先思考一个问题,那就是为什么要上锁?然后我要如何选择锁?锁具体如何实现? 在文章得末尾我给出了我的个人答案. 一.什么是悲观锁? 1.悲观锁就是在操作数据时,认为此操作会出现数据冲 ...

  3. Kubelet bootstrap认证配置步骤

    kubelet 授权 kube-apiserver 的一些操作 exec run logs 等 RBAC 只需创建一次就可以 kubectl create clusterrolebinding kub ...

  4. 【测试工具】Macaca 自动遍历器 NoSmoke

    Macaca 提供的基础能力上研发出了一套多端深度遍历爬虫工具. 希望可以最大化减少UI 测试脚本的编写涵盖以下功能点: 支持iOS, Android,PC-Web 三个平台的自动化测试 同时可以通过 ...

  5. Nginx(二)

    利用include功能优化nginx的配置文件 [root@lnmp conf]# cat nginx.conf worker_processes 1; events {     worker_con ...

  6. FastFDS基础

    1. FastDFS介绍 FastDFS( Fast Distributed file system)是一款轻量级的.高性能的.阿里巴巴开源的分布式文件系统.该系统的作者是余庆 (happyfish1 ...

  7. python历史与基本类型

    前言 我自学的方式主要是看文档,看视频,第一次做写博客这么神圣的事情,内心是忐忑的,写的东西比较杂,路过的小伙伴不要嘲笑我,主要是记录一日所学,顺便锻炼一下语言组织能力吧,anyway,这些都不重要, ...

  8. Codeforces 1082C Multi-Subject Competition 前缀和 A

    Codeforces 1082C Multi-Subject Competition https://vjudge.net/problem/CodeForces-1082C 题目: A multi-s ...

  9. 使用GetAdaptersInfo时,网卡类型的值为71

    使用GetAdaptersInfo时,网卡类型的值为71,代表无线网卡.

  10. 2019.02.28 bzoj4199: [Noi2015]品酒大会(sam+线段树)

    传送门 题意:给一个串,每个位置有一个权值,当S[s...s+len−1]=S[t...t+len−1]&&S[s...s+len]̸=S[t..t+len]S[s...s+len-1 ...