字符串匹配的三種方法

  字符串匹配,實現c++ strstr()函數c++

1.蠻力法

 1 int strStr(string haystack, string needle) {
 2         
 3         int i, hSize = haystack.size(), nSize = needle.size();
 4         if(hSize < nSize)
 5             return -1;
 6         if(nSize == 0)
 7             return 0;
 8         for(i = 0; i <= hSize - nSize && haystack.substr(i, nSize) != needle; ++i);
 9         
10         return i <= hSize - nSize ? i : -1;
11     }

2.Robin Karp

  具體說明參考維基百科:https://en.wikipedia.org/wiki/Rabin–Karp_algorithm算法

 1 char hash(const string& str)
 2     {
 3         char all = 0;
 4         for(auto c : str)
 5             all ^= c;
 6         return all;
 7     }
 8 
 9 //選定一個hash函數,對字符串hash,hash值不一樣必定是不一樣字符串
10     //因爲hash值可能有衝突 因此hash值相同的字符並不必定相同 須要逐個字符再比較 
11     //hash函數能夠本身寫,也能夠用std::hash<string>
12     int strStr(string haystack, string needle) {
13         
14         int i, hSize = haystack.size(), nSize = needle.size();
15         if(hSize < nSize)
16             return -1;
17         if(nSize == 0)
18             return 0;
19         //或者使用std::hash
20         //std::hash<string> hash;
21         char target = hash(needle);
22         for(i = 0; i <= hSize - nSize; ++i)
23         {
24             if(hash(haystack.substr(i,nSize)) == target && haystack.substr(i,nSize) == needle)
25                 break;
26         }
27         
28         return i <= hSize - nSize ? i : -1;
29     }

3.kmp

  具體說明參考維基百科:https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm函數

 1 vector<int> buildNextArray(const string& s)
 2     {
 3         vector<int> next(s.size());
 4         int i = 2, j = 0;
 5         next[0] = -1;
 6         if(s.size() > 1)
 7             next[1] = 0;
 8         while(i < s.size())
 9         {
10             if(s[i-1] == s[j])
11                 next[i++] = ++j;
12             else if(j > 0)
13                 j = next[j];
14             else
15                 next[i++] = 0;
16         }
17         return next;
18     }
19 
20 int strStr(string haystack, string needle) {
21         
22         int start = 0, i = 0, hSize = haystack.size(), nSize = needle.size();
23         if(hSize < nSize)
24             return -1;
25         if(nSize == 0)
26             return 0;
27         //kmp算法
28         vector<int> next = buildNextArray(needle);
29         while(start <= hSize - nSize)
30         {
31             if(haystack[start + i] == needle[i])
32             {
33                 if(++i == nSize)
34                     return start;
35             }
36             else
37             {
38                 start = start + i - next[i];
39                 i = i > 0 ? next[i] : 0;
40             }
41         }
42         
43         return -1;
44     }
相關文章
相關標籤/搜索