【题目】

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. java8 - 时间

    import java.time.DayOfWeek; import java.time.Duration; import java.time.Instant; import java.time.Lo ...

  2. Sample ASP.NET IHttpHandler

    LoggerHandler.cs using System; using System.Collections.Generic; using System.Diagnostics; using Sys ...

  3. 【LOJ】 #2665. 「NOI2013」树的计数

    题解 我们统计深度对于bfs序统计,树结构出现分歧的地方必然是BFS序的最后一段,这个最后一段同时还得是dfs序上连续的一段 如果不是bfs序的最后一段,那么必然下一层会有节点,如果树结构分歧了,那么 ...

  4. thinkphp在某种方法之前与之后执行的方法

    在thinkphp中有两个方法可以在某个方法之前或之后执行,分别是_before_xxx() 和_after_xxx()两个方法 1 2 3 4 5 6 public function _before ...

  5. 在qemu环境中用gdb调试Linux内核

    简介 对用户态进程,利用gdb调试代码是很方便的手段.而对于内核态的问题,可以利用crash等工具基于coredump文件进行调试.其实我们也可以利用一些手段对Linux内核代码进行gdb调试,qem ...

  6. join和 Daemon守护线程

    一.前言 一个程序至少有一个主线程,主线程启动子线程后,它们之间并没有隶属关系.主线程和子线程执行是并行的,相互独立.主线程执行完毕后默认不等子线程执行结束就接着往下走了,如果有其他程序就会运行另外的 ...

  7. 设计模式 结构型模式 外观模式(Facade Pattern)

    在软件开发过程中,客户端程序经常会与复杂系统的内部子系统进行耦合,从而导致客户端程序随着子系统的变化而变化. 这时为了将复杂系统的内部子系统与客户端之间的依赖解耦,从而就有了外观模式,也称作 ”门面“ ...

  8. 【assembly】用汇编写的一个BMP图片读取器

    ;----------------------------- ;文件满足256色调的 ;----------------------------- Stack    Segment           ...

  9. Codeforces Round #228 (Div. 1) A. Fox and Box Accumulation 贪心

    A. Fox and Box Accumulation 题目连接: http://codeforces.com/contest/388/problem/A Description Fox Ciel h ...

  10. poj 2623 Sequence Median 堆的灵活运用

    I - Sequence Median Time Limit:1000MS     Memory Limit:1024KB     64bit IO Format:%I64d & %I64u ...