博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
KMP string pattern matching
阅读量:4957 次
发布时间:2019-06-12

本文共 3052 字,大约阅读时间需要 10 分钟。

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;	}

 

转载于:https://www.cnblogs.com/kid551/p/4370387.html

你可能感兴趣的文章
查看端口占用cmd命令
查看>>
2019.01.17王苛震作业
查看>>
解决package jdk1.8-2000:1.8.0_171-fcs.x86_64 is already installed问题
查看>>
XPath Helper和XPath语法
查看>>
转载 java学习注意点
查看>>
Halcon学习(八)文本操作
查看>>
收缩数据库及修改数据库名称
查看>>
MFC电子词典
查看>>
简单工厂(Simple Factory)
查看>>
04: 打开tornado源码剖析处理过程
查看>>
02: 安装epel 解决centos7无法使用yum安装nginx
查看>>
清除浮动
查看>>
PayPal(贝宝)支付接口、文档、IPN
查看>>
站立会议总结07
查看>>
ORACLE 10G R2_执行计划中cost cardinality bytes cpu_cost io_cost解释
查看>>
全站 HTTPS
查看>>
Topic: Cost Element talks
查看>>
关于this和base
查看>>
(转)Scrapy 深入一点点
查看>>
荧光激活细胞分选( FACS)
查看>>