ZP1015

当你的才华还撑不起你的野心时,就应该静下心来学习; 当你的能力还驾驭不了你的目标时,就应该沉下心来历练...

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

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



阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xy010902100449/article/details/49949867
文章标签: LeetCode kmp 算法
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

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

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭