KMP实现
class Solution {
public:
void getNext(int* next, string& needle){ //int*
int j = 0;
next[0] = 0;
for(int i = 1; i < needle.size(); i++){
while(j > 0 && needle[i] != needle[j]){
j = next[j - 1];
}
if(needle[i] == needle[j]){
j++;
}
next[i] = j;
}
}
int strStr(string haystack, string needle) {
if(needle.size() == 0) return 0;
int next[needle.size()];
getNext(next, needle);
int j = 0;
int i = 0;
for(i = 0; i < haystack.size(); i++){
while(j > 0 && haystack[i] != needle[j]){
j = next[j - 1];
}
if(haystack[i] == needle[j]){
j++;
//i++;
}
if(j == needle.size()) return (i - needle.size() + 1);
}
return -1;
}
};