[LeetCode28] Implement strStr()

题目:字符串匹配

Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

分类:String Two Pointers

代码:

 1 class Solution {
 2 private:
 3     char hash(const string& str)
 4     {
 5         char all = 0;
 6         for(auto c : str)
 7             all ^= c;
 8         return all;
 9     }
10     
11     vector<int> buildNextArray(const string& s)
12     {
13         vector<int> next(s.size());
14         int i = 2, j = 0;
15         next[0] = -1;
16         if(s.size() > 1)
17             next[1] = 0;
18         while(i < s.size())
19         {
20             if(s[i-1] == s[j])
21                 next[i++] = ++j;
22             else if(j > 0)
23                 j = next[j];
24             else
25                 next[i++] = 0;
26         }
27         return next;
28     }
29     
30 
31 public:
32     /*
33     //brute-force
34     int strStr(string haystack, string needle) {
35         
36         int i, hSize = haystack.size(), nSize = needle.size();
37         if(hSize < nSize)
38             return -1;
39         if(nSize == 0)
40             return 0;
41         for(i = 0; i <= hSize - nSize && haystack.substr(i, nSize) != needle; ++i);
42         
43         return i <= hSize - nSize ? i : -1;
44     }*/
45     
46 /*
47     //robin karp 
48     //followed WiKI with a simple hash function, https://en.wikipedia.org/wiki/Rabin–Karp_algorithm)
49     //选定一个hash函数,对字符串hash,hash值不同一定是不同字符串
50     //由于hash值可能有冲突 所以hash值相同的字符并不一定相同 需要逐个字符再比较 
51     //hash函数可以自己写,也可以用std::hash<string>
52     int strStr(string haystack, string needle) {
53         
54         int i, hSize = haystack.size(), nSize = needle.size();
55         if(hSize < nSize)
56             return -1;
57         if(nSize == 0)
58             return 0;
59         //或者使用std::hash
60         //std::hash<string> hash;
61         char target = hash(needle);
62         for(i = 0; i <= hSize - nSize; ++i)
63         {
64             if(hash(haystack.substr(i,nSize)) == target && haystack.substr(i,nSize) == needle)
65                 break;
66         }
67         
68         return i <= hSize - nSize ? i : -1;
69     }
70  */
71 
72    //kmp
73    int strStr(string haystack, string needle) {
74         
75         int start = 0, i = 0, hSize = haystack.size(), nSize = needle.size();
76         if(hSize < nSize)
77             return -1;
78         if(nSize == 0)
79             return 0;
80         //kmp算法
81         vector<int> next = buildNextArray(needle);
82         while(start <= hSize - nSize)
83         {
84             if(haystack[start + i] == needle[i])
85             {
86                 if(++i == nSize)
87                     return start;
88             }
89             else
90             {
91                 start = start + i - next[i];
92                 i = i > 0 ? next[i] : 0;
93             }
94         }
95         
96         return -1;
97     }
98 
99 };

 

posted @ 2016-08-10 23:19  zhangbaochong  阅读(180)  评论(0编辑  收藏  举报