KMP
class Solution {
public:char *strStr(char *haystack, char *needle) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int hlen,nlen,*Fail,i,j;
hlen = strlen(haystack),nlen = strlen(needle);
Fail = new int[nlen];
Fail[0] = -1, j = -1,i = 0;
while(i<nlen-1){
if(needle[i]==needle[j]||j==-1){
i++,j++;
Fail[i] = j;
}
else
j = Fail[j];
}
i = 0, j = 0;
while(i<hlen&&j<nlen){
if(haystack[i]==needle[j]||j==-1){
i++,j++;
}
else
j = Fail[j];
}
delete [] Fail;
if(j==nlen)
return haystack+i-j;
return NULL;
}
};