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. kettle 无法正常启动问题

    Java环境配置后,双击spoon.bat无法打开 修改spoon.bat里内存配置: if "%PENTAHO_DI_JAVA_OPTIONS%"=="" s ...

  2. Oracle表中的主键被当成哪些表的外键

    SELECT B.TABLE_NAME FROM USER_CONSTRAINTS A INNER JOIN USER_CONS_COLUMNS B ON A.CONSTRAINT_NAME = B. ...

  3. django 的model是如何把字段加入到meta中的

    def contribute_to_class(self, cls, name): self.set_attributes_from_name(name) self.model = cls cls._ ...

  4. RK3288 mipi屏参数配置文件

    RK3288     Android 5.1系统     Linux 3.10 mipi屏参数配置文件所在的路径:kernel/arch/arm/boot/dts/xxx_mipi.dtsi 屏参数配 ...

  5. 与引导文件系统/vmfs/devices..的备用设备之间的连接已丢失,主机配置更改将不会保存到持久存储中

    Cisco UCS 刀片服务器与NETAPP存储 1.异常问题描述: 2.可能原因:存储链路异常     比如断电恢复.光纤线本身的问题.模块的问题.环境温度的问题.bug之类的都有可能 3.处理: ...

  6. tcp中delay_ack的理解

    内核版本,3.10. 首先,我们需要知道,在一个sock中,维护ack的就有很多变量,多种状态: struct inet_connection_sock { .... __u8 icsk_ca_sta ...

  7. 写出良好风格的JS、CSS代码

    现在代码的格式都有 eslint.prettier.babel 这些来保证,但是技术手段再高端都不能解决代码可读性的问题. 因为这个只有个人才能解决.但是注意一下事项,可以显著提高代码的可读性.可识别 ...

  8. Dubbo helloword

    首先,开始编写服务提供者的api接口, SampleService  接口 package bhz.dubbo.sample.provider; import java.util.List; publ ...

  9. 将Promise融会贯通之路

    前端初学者经常会问,我如何在ajax1结束之后才启动ajax2呢?我怎么做才能在所有的ajax结束之后触发某程序呢?亦或是哎真是烦,5个ajax套在一起,原来的逻辑是什么呀! 一个稍微有点经验的前端程 ...

  10. 正则冷知识;分组捕获、replace()的用法...

    1.var reg=/./; var reg=/\./的区别?? 前者代表任意一个字符,后者代表这个字符串中得有一个.. 2.?的使用?? 如果单独的一个字符串后面带? , var reg=/\d?/ ...