[LeetCode-28] Implement strStr(KMP 字符串匹配)

原创 2015年11月20日 17:59:41

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Subscribe to see which companies asked this question

【解题思路】

采用 字符串匹配算法 KMP 算法的思想

void getNext(char *strSub,int *strSubnext)  
{
	if(!strSub||!strSubnext)
		return;

	int strSubLen = strlen(strSub);

	int i = 0;/*后缀字符的当前位置*/
	int j = -1;/*前缀字符的当前位置*/
	
	strSubnext[0] = -1;
	
	while(i<(strSubLen-1)) {
		if(j==-1||strSub[i]==strSub[j]) { /*后缀字符和前缀字符相等时*/
			i++;
			j++;
			strSubnext[i] = j;/*前缀字符当前对应值是在后缀位置+1*/
		} else {
			j = strSubnext[j];/*回溯回去,回到后缀位置*/
		}
	}
}

int strStr(char* haystack, char* needle)
{
	if(!haystack||!needle) {
		return -1;
	}

	int srcStringLen = strlen(haystack);
	int keyStringLen = strlen(needle);

	if(srcStringLen<keyStringLen)
		return -1;
		
	if(srcStringLen==0||keyStringLen==0)
		return 0;
		
	int *strSubnext = (int *)malloc(sizeof(int)*(keyStringLen));
	
	int i = 0;
	int j = 0;

	getNext(needle,strSubnext);

	while(i<srcStringLen&&j<keyStringLen) {
		if(j==-1||haystack[i]==needle[j]) {
			++i;
			++j;
		} else {
			j = strSubnext[j];
		}

		if(j==keyStringLen) {
			free(strSubnext);
			return (i-keyStringLen);/*返回当前位置*/
		}
	}

	return -1; /*没找到*/
}

完整代码如下:

// LeetCode28-Implement strStr.cpp : 定义控制台应用程序的入口点。

//LeetCode28-Implement strStr
//Written by ZP1015
//2015.11.20
#include "stdafx.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<time.h>

void getNext(char *strSub,int *strSubnext)  
{
	if(!strSub||!strSubnext)
		return;

	int strSubLen = strlen(strSub);

	int i = 0;/*后缀字符的当前位置*/
	int j = -1;/*前缀字符的当前位置*/
	
	strSubnext[0] = -1;
	
	while(i<(strSubLen-1)) {
		if(j==-1||strSub[i]==strSub[j]) { /*后缀字符和前缀字符相等时*/
			i++;
			j++;
			strSubnext[i] = j;/*前缀字符当前对应值是在后缀位置+1*/
		} else {
			j = strSubnext[j];/*回溯回去,回到后缀位置*/
		}
	}
}

int strStr(char* haystack, char* needle)
{
	if(!haystack||!needle) {
		return -1;
	}

	int srcStringLen = strlen(haystack);
	int keyStringLen = strlen(needle);

	if(srcStringLen<keyStringLen)
		return -1;
		
	if(srcStringLen==0||keyStringLen==0)
		return 0;
		
	int *strSubnext = (int *)malloc(sizeof(int)*(keyStringLen));
	
	int i = 0;
	int j = 0;

	getNext(needle,strSubnext);

	while(i<srcStringLen&&j<keyStringLen) {
		if(j==-1||haystack[i]==needle[j]) {
			++i;
			++j;
		} else {
			j = strSubnext[j];
		}

		if(j==keyStringLen) {
			free(strSubnext);
			return (i-keyStringLen);/*返回当前位置*/
		}
	}

	return -1; /*没找到*/
}

int main()
{
	clock_t start,stop;
	double duringtime;
	start = clock();
	char *srcString ="aaaab";
	char *keyString = "b";

	int result = strStr(srcString, keyString);
	printf("%d\n",result);
	stop = clock();
	duringtime=(double)(stop-start)/CLOCKS_PER_SEC;
	printf("%f\n",duringtime);

	getchar();
	return 0;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

C++ 递归实现n的阶乘

递归在学习的过程中不容易被理解,能够用递归解决问题的有两个条件: 1、有结束的条件(递归的出口) 2、函数体能够被反复的执行。下面是C++递归实现阶乘的代码:#include using name...

21. Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t...

LeetCode Implement strStr()(朴素的字符串匹配,RK算法,KMP算法)

这次来个大整合,因为正好处理到经典的字符串匹配问题 那么也是用c语言实现,现在开始吧 Implement strStr(). Returns the index of the first occ...

28. Implement strStr()(String字符串匹配)

题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if...

LeetCode_28---Implement strStr() [KMP算法,字符匹配算法]

Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle...

[Leetcode P28] Implement strStr()(KMP算法)

原题:Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if need...

Leetcode 28. Implement strStr() KMP算法解题

Implement strStr().   Returns the index ofthe first occurrence of needle in haystack, or -1 if nee...

Leetcode 28.Implement strStr() 解题报告【C库函数strstr()模拟-字符串中子串首次出现的地址】

28. Implement strStr()   提交网址 https://leetcode.com/problems/implement-strstr/ Total Accepted: ...

[LeetCode]28 实现子字符串匹配

Implement strStr() (实现子字符串匹配)【难度:Easy or Medium】 Implement strStr().Returns the index of the first ...

28. Implement strStr() 以及KMP算法的实现

28. Implement strStr()题目描述Implement strStr().Returns the index of the first occurrence of needle in ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[LeetCode-28] Implement strStr(KMP 字符串匹配)
举报原因:
原因补充:

(最多只允许输入30个字)