码迷,mamicode.com
首页 > 其他好文 > 详细

LeetcodeOJ: Implement strStr() [ Boyer–Moore string search algorithm ]

时间:2015-02-18 14:05:12      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

 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     // search
15     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 };

 

LeetcodeOJ: Implement strStr() [ Boyer–Moore string search algorithm ]

标签:

原文地址:http://www.cnblogs.com/ydlme/p/4295725.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!