strStr

来源:互联网 发布:C 先序遍历的递归算法 编辑:程序博客网 时间:2024/06/06 04:36

strstr (a.k.a find sub string), is a useful function in string operation. Your task is to implement this function.

For a given source string and a target string, you should output the firstindex(from 0) of target string in source string.

If target does not exist in source, just return -1.

Have you met this question in a real interview? 
Yes
Example

If source = "source" and target = "target", return -1.

If source = "abcdabcdefg" and target = "bcd", return 1.

Challenge

O(n2) is acceptable. Can you implement an O(n) algorithm? (hint: KMP)

Clarification

Do I need to implement KMP Algorithm in a real interview?

  • Not necessary. When you meet this problem in a real interview, the interviewer may just want to test your basic implementation ability. But make sure your confirm with the interviewer first.

题目至少有3种方法,第一种是暴力搜索,每次移动一步,则时间复杂度为O(nm),其中n表示source的长度,m表示target的长度。第二种方法是在第一种方法的基础上做了改进,每次对比时,同时从两边向中间夹,这样可以适当降低时间复杂度;第三种方法,肯定就是KMP了,KMP算法优化的是第一二种方法存在的回溯问题,第一二种方法中,每次移动i到达某个值,使得source[i] != target[j]的时候,会回溯到i - j + 1的位置,也就是说[i-j+1, i]这个区间又要重复计算;

KMP算法的提出就是要避免此回溯,使得i只往后移,也就是说,这样的时间复杂度将是O(N),N为source的长度。KMP的算法证明,就不详细展开了;这里主要讲下next[j]的求解,其实在求解next[j]的过程有递归的含义在其中,也是在做一个字符串的匹配问题,有两个变量k,j,分别初始化为k = -1, j = 0,next[0] = -1;当k == -1 或者 target[k] == target[j]的时候,则说明next[j + 1] = k + 1; 否则的话,则将k移动到next[k],这一步就包括了递推的字符串匹配在其中,理解起来比较难;

class Solution {public:    /**     * Returns a index to the first occurrence of target in source,     * or -1  if target is not part of source.     * @param source string to be scanned.     * @param target string containing the sequence of characters to match.     */    //KMP算法    void GetNext(const char *p, vector<int> &next)    {        int len = strlen(p);        int k = -1, j = 0;        while(j < len - 1)        {            if(k == -1 || p[k] == p[j])            {                next[++j] = ++k;            }            else                k = next[k];        }    }            int strStr(const char *source, const char *target) {        // write your code here        if(source == NULL || target == NULL)            return -1;        int lens = strlen(source);        int lent = strlen(target);        if(lens < lent)            return -1;        int i = 0, j = 0;        vector<int> next(lent, -1);        GetNext(target, next);        while(i < lens && j < lent)        {            if(j == -1)            {                ++i;                j = 0;            }            else if(source[i] == target[j])            {                ++i;                ++j;            }            else             {                j = next[j];            }        }                if(j >= lent)            return i - j;        return -1;    }};

 

0 0
原创粉丝点击