1 class Solution { 2 public: 3 int strStr(char *haystack, char *needle) { 4 5 int i = 0 , skip[256]; 6 char *str = haystack, *substr = needle; 7 int len_src = strlen(str), len_sub = strlen(substr); 8 // preprocess 9 for (i = 0; i < 256; i++)10 skip[i] = len_sub;11 int last = len_sub - 1;12 for (i = 0; i < last;i++)13 skip[substr[i]] = last - i;14 // search15 int pos = 0, j;16 while (pos <= len_src-len_sub) {17 j = last;18 while (j>=0 && str[pos+j]==substr[j])19 j--;20 if (j<0) 21 return pos;22 pos += skip[str[pos+last]];23 }24 return -1;25 }26 };