[LeetCode-28] Implement strStr(KMP 字符串匹配)
来源:互联网 发布:ps做淘宝主图画布 编辑:程序博客网 时间:2024/05/22 14:00
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;}
1 0
- [LeetCode-28] Implement strStr(KMP 字符串匹配)
- Implement strStr() 字符串匹配(KMP)
- 【KMP&字符串匹配】Implement strStr()
- LeetCode-28-Implement strStr()(字符串匹配)
- [LeetCode]Implement strStr() 字符串匹配
- leetcode 字符串匹配Implement strStr()
- (LeetCode)Implement strStr() --- 匹配字符串
- leetcode 28: Implement strStr() (KMP算法)
- 28-Implement strStr()(匹配字符串问题KMP算法)
- Implement strStr() (匹配字符串问题,哈希解法) 【leetcode】
- LeetCode: Implement strStr()(字符串匹配:Sunday算法)
- LeetCode Implement strStr() --- KMP
- Implement strStr() [LeetCode] + KMP
- kmp-----leetcode(Implement strStr() )
- LeetCode Implement strStr()(朴素的字符串匹配,RK算法,KMP算法)
- 【字符串匹配】Implement strStr()
- 字符串匹配Implement strStr()
- Leetcode 28 Implement strStr() KMP算法
- 插入mysql时重复数据处理方法
- Android ImageView 详述
- Unity3d 调试
- Nginx配置https双向认证
- Flume采集数据到HDFS时,生成的文件中,开头信息有乱码
- [LeetCode-28] Implement strStr(KMP 字符串匹配)
- PHP接口API文档转换SDK【适配ThinkPHP框架】
- 北大OJ1006
- 贪心法实现无向图的划分 代码
- UIPickerView的点滴
- flask-学习目的与开发环境搭建
- java垃圾回收之Minor GC和Major GC
- 【C++常识】C++的iostream标准库介绍 (转)
- GreenPlum环境搭建