【题目】

Implement strStr().

Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.

【题意】

实现库函数strStr(), 功能是在字符串haystack中找出目标串needle第一次出现的索引位

【思路】

字符串的匹配,能够用暴力解法,但不推荐。一般使用KMP算法求解。

简要介绍一下KMP的思想:

haystack是题设所给的匹配串,needle是题设所给的模式串。

needle在和haystack匹配时,假设遇到某个位置上的字符失配,暴力法会将needle的游标返回头部,从头開始又一次匹配;而KMP算法同意游标不返回头部,而利用已有的匹配结果,将游标移动到新的位置k上,从k位置開始匹配。      

    怎样确定这个新位置,或者说这个新位置是什么呢?

   (1).我们这么来看,如果在当前的匹配过程中发现haystack[i]!=needle[j],显然这个时候haystack[i-j, i-1]和needle[0,j-1]各个位置上的字符已经匹配。

   (2).此时,needle新的游标位置next[j]=k,它满足子串needle[0,k-1]与子串needle[j-k,j-1]全然匹配,也就是说字符串needle[0,j-1]的前k个字符与后k个字符同样。【k是满足该匹配条件的最大值,且k<j】

   (3)把needle的游标从j移动到k,意味着我们从k開始比較就能够了,needle[0,k-1]不须要再反复比較了。这一点由(1)保证。

结合(1)(2)我们有:haystack[i-j, i-1]=needle[0,j-1] && needle[0,k-1]=needle[j-k, j-1]

显然我们能够推出:needle[0,k-1]=haystack[i-k,i-1]; 也即从k位置開始比較就可以,不须要从头開始匹配。

        

    

    KMP算法关键在于建立next数组,即发生不匹配情况是回跳到哪个位置继续匹配。

    如果我们已经确定了needle字符串中j位置的字符失配时跳转的位置是k,即next[j]=k, 依据上面的分析我们知道此时满足needle[0,k-1]=needle[j-k, j-1]。那next[j+1]=?

    1. 假设needle[k]==needle[j],显然有needle[0,k]=needle[j-k, j],则next[j+1]=k+1;

    2. 假设needle[k]!=needle[j], 这也是一种失配的情况(即needle起始字符串和结尾字符串匹配时发生失配),则游标k应该跳转到next[k],然后再与needle[j]比較,假设还是失配,游标继续通过next数组回跳,直至与needle[j]匹配,此时next[j+1]=k+1;

【代码】

class Solution {
public:
void getNext(char*needle, int*next){
int size=strlen(needle);
int i=0,k=-1;
next[0]=-1;
while(i<size-1){ //由于我们通过A[i]来求A[i+1]的,因此结束条件是i<size-1
if(k==-1||needle[i]==needle[k]){
i++;
k++;
next[i]=k;
}
else{
k=next[k];
}
}
}
char *strStr(char *haystack, char *needle) {
if(needle==NULL || haystack==NULL)return NULL;
int patternSize=strlen(needle);
int haystackSize=strlen(haystack);
int*next=new int[patternSize];
//生成next数组
getNext(needle,next);
//匹配
int i=0; //haystack的游标
int j=0; //needle的游标
while(i<haystackSize&&j<patternSize){
if(j==-1||haystack[i]==needle[j]){
i++;j++;
}
else{
j=next[j];
}
}
delete next;
if(j==patternSize)return haystack+i-j;
return NULL;
}
};

LeetCode: Implement strStr() [027]的更多相关文章

  1. [LeetCode] Implement strStr() 实现strStr()函数

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

  2. [Leetcode] implement strStr() (C++)

    Github leetcode 我的解题仓库   https://github.com/interviewcoder/leetcode 题目: Implement strStr(). Returns ...

  3. LeetCode Implement strStr()(Sunday算法)

    LeetCode解题之Implement strStr() 原题 实现字符串子串匹配函数strStr(). 假设字符串A是字符串B的子串.则返回A在B中首次出现的地址.否则返回-1. 注意点: - 空 ...

  4. [LeetCode] Implement strStr()

    Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...

  5. leetcode——Implement strStr() 实现字符串匹配函数(AC)

    Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if need ...

  6. leetcode implement strStr python

    #kmp class Solution(object): def strStr(self, haystack, needle): """ :type haystack: ...

  7. LeetCode Implement strStr() 实现strstr()

    如题 思路:暴力就行了.1ms的暴力!!!别的牛人写出来的,我学而抄之~ int strStr(char* haystack, char* needle) { ; ; ; ++i) { ; ; ++j ...

  8. [Leetcode][Python]28: Implement strStr()

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...

  9. 【一天一道LeetCode】#28. Implement strStr()

    一天一道LeetCode系列 (一)题目 Implement strStr(). Returns the index of the first occurrence of needle in hays ...

随机推荐

  1. day6 ConfigParser模块 yaml模块

        yaml模块: python可以处理yaml文件,yaml文件安装的方法为:$ pip3 install pyyaml    configparser模块,用来处理文件的模块,可以实现文件的增 ...

  2. 003 Scipy库简介

    参考文档补充原本的文档: https://www.cnblogs.com/mrchige/p/6504324.html 一:原本的简单介绍 1.Scipy库 Scipy库是基于python生态的一款开 ...

  3. poj1703 Find them, Catch them(带权并查集)

    题目链接 http://poj.org/problem?id=1703 题意 有两个帮派:龙帮和蛇帮,两个帮派共有n个人(编号1~n),输入m组数据,每组数据为D [a][b]或A [a][b],D[ ...

  4. Vue中directives的用法

    关于 vue 中 directives 的用法问题,详细可以参考vue官方对directives的解释 当前文章主要讲述directives怎么用,directives做权限按钮的功能 ###1. d ...

  5. caffe 中 plot accuracy和loss, 并画出网络结构图

    plot accuracy + loss 详情可见:http://www.2cto.com/kf/201612/575739.html 1. caffe保存训练输出到log 并绘制accuracy l ...

  6. 跟厂长学PHP7内核(三):源码目录结构

    上篇文章我们已经介绍了源码分析工具的安装.配置以及调试方法,本文我们来讲述一下PHP源码的目录结构. 一.目录概览 以php-7.0.12为例,看过源码的同学们应该发现源码目录多达十多个,下面是每个目 ...

  7. CentOS 7下MySQL5.7.23的服务配置参数测试

    CentOS 7默认安装MySQL5.7.23,服务管理发生了变化,从sysvinit(service mysql start)变化为systemd(systemctl start mysqld.se ...

  8. Java时间间隔问题在Android中的使用

    转载请注明出处:http://www.cnblogs.com/cnwutianhao/p/6606720.html 假设我们在做项目的时候,获取到了一段音频,也知道音频长度,那么我们想对音频做一些处理 ...

  9. web计时机制

    前面的话 页面性能一直都是Web开发人员比较关注的领域.但在实际应用中,度量页面性能的指标,是javascript的Date对象.Web Timing API改变了这个局面,让开发人员通过javasc ...

  10. [ 转载 ] Java基础13--equals方法

    一.equals方法介绍 1.1.通过下面的例子掌握equals的用法 1 package cn.galc.test; 2 3 public class TestEquals { 4 public s ...