您的位置:首页 > 其它

[Leetcode] Implement strStr()

2014-04-12 18:19 295 查看
Implement strStr().

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

KMP算法!

class Solution {
public:
void getNext(vector<int> &next, string &needle) {
int i = 0, j = -1;
next[i] = j;
while (i != needle.length()) {
while (j != -1 && needle[i] != needle[j]) j = next[j];
next[++i] = ++j;
}
}
int strStr(string haystack, string needle) {
if (haystack.empty()) return needle.empty() ? 0 : -1;
if (needle.empty()) return 0;
vector<int> next(needle.length() + 1);
getNext(next, needle);
int i = 0, j = 0;
while (i != haystack.length()) {
while (j != -1 && haystack[i] != needle[j]) j = next[j];
++i; ++j;
if (j == needle.length()) return i - j;
}
return -1;
}
};


上面的是通常用的KMP算法,但是算法是有一定缺陷的。比如我们的模式串 pattern =“AAAAB”,其中很容易得到next数组为01230。如果目标匹配串为 “AAAACAAAAB” ,大家可以模拟一下,A要回溯多次。就是说我们的next数组优化并不彻底。优化算法:next[i]表示匹配串在i处如果匹配失败下次移到的位置。下面是优化后的的求next数组的代码。虽然两种写求得next值不一样,但是kmp函数的写法是一样的。

class Solution {
public:
void getNext(vector<int> &next, string &needle) {
int i = 0, j = -1;
next[i] = j;
while (i < needle.length() - 1) {
while (j != -1 && needle[i] != needle[j]) j = next[j];
++i; ++j;
//特殊情况,这里即为优化之处。考虑下AAAAB, 防止4个A形成0123在匹配时多次迭代。
if (needle[i] == needle[j]) next[i] = next[j];
else next[i] = j;
}
}
int strStr(string haystack, string needle) {
if (haystack.empty()) return needle.empty() ? 0 : -1;
if (needle.empty()) return 0;
vector<int> next(needle.length() + 1);
getNext(next, needle);
int i = 0, j = 0;
while (i != haystack.length()) {
while (j != -1 && haystack[i] != needle[j]) j = next[j];
++i; ++j;
if (j == needle.length()) return i - j;
}
return -1;
}
};


下面不是KMP算法,但是代码特别简洁:

class Solution {
public:
int strStr(string haystack, string needle) {
int i, j;
for (i = j = 0; i < haystack.size() && j < needle.size();) {
if (haystack[i] == needle[j]) {
++i; ++j;
} else {
i -= j - 1; j = 0;
}
}
return j != needle.size() ? -1 : i - j;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: