This is a creation in
Article, where the information may have evolved or changed.
Found a lot of questions, all asked is the library function. To the simple, inefficient algorithm, to the complex to do, it depends on the source.
Write a function that looks for a substring (m) in the first position in a string (n)
Violent string matching method (Brute forceing matching). This notation is not difficult, the complexity O (n*m).
func IndexFuck(s, sep string) int {for i := 0; i < len(s); i++ {if s[i] == sep[0] {is := truej := 0for ; j < len(sep); j++ {if sep[j] != s[i] {is = falsebreak}}if is {return i}}}return -1}
The RABIN-KARP algorithm can minimize the complexity to O (n). Set the substring to match to Sep, and the string to find is S. Using a hashing algorithm, the SEP is hashed into a number, in the length of Sep, traversing the same string of length and Sep in s, until the calculated value is equal.
The hash is computed by taking the original string as an e-binary encoding, and then turning the encoding into a 10-binary representation of the number. Golang uses 16777619 as the binary number. (This is my understanding, as to why I chose this integer, I am not very clear, but found that many hashing algorithms have used this magical number 16777619 = (2^24 + 403)).
For example, the original string s is 12345, and the matching string Sep is 45. For the first time, the hash value of the string is computed by comparing 12 and 45 1*E+2 != 4*E+5 . The values of 23 and 45 are then computed.
Here is a question, if the string to match is longer, 1000 bits, then each match has to be calculated for the 1000 bits. At this point the complexity is O (n*m), but also need to calculate, which is more complicated than the brute force judgment algorithm. Careful observation, found that each calculation of the length of the same, and in turn, the front and back two strings 12 and 23 have a common Part 2, if the substring of sep length is 1000, then the common part of the two sub-string is 999 length. Therefore, only need to keep the common part, the front of the head removed, the back of the tail to fill up, you can complete the calculation of the new string.
The above example, (1*E+2)*E+3-1*E*E = 2*E+3 . In this way, a simple operation is able to get the hash value of the new string. Golang inside the strings package implementation:
const primeRK = 16777619// hashstr returns the hash and the appropriate multiplicative// factor for use in Rabin-Karp algorithm.func hashstr(sep string) (uint32, uint32) {hash := uint32(0)for i := 0; i < len(sep); i++ {hash = hash*primeRK + uint32(sep[i])}var pow, sq uint32 = 1, primeRKfor i := len(sep); i > 0; i >>= 1 {if i&1 != 0 {pow *= sq}// 只有32位,超出范围的会被丢掉sq *= sq}return hash, pow}func Index(s, sep string) int {n := len(sep)// Hash sep.hashsep, pow := hashstr(sep)var h uint32for i := 0; i < n; i++ {h = h*primeRK + uint32(s[i])}if h == hashsep && s[:n] == sep {return 0}for i := n; i < len(s); {h *= primeRKh += uint32(s[i])h -= pow * uint32(s[i-n])i++if h == hashsep && s[i-n:i] == sep {return i - n}}return -1}
At the end of the for loop, I means to calculate the new string of new characters, which is 3 in the example. So that i-n+1 's the new string header address.
In general, the values calculated by this algorithm will exceed the range of shaping. Above the 16777619, calculate the square on unit32 the Super range. A common practice is to take a larger prime number, to seek redundancy, and to use the remainder as the hash value. This ensures that the high position is not intercepted and discarded. And the code in the Golang package is not operational, directly discarded, too domineering.
Please refer to the complete source code in this article.
Reference documents
- "1" illustrated Rabin-karp string search algorithm-Turing community
- Rabin-karp algorithm of "2" string matching-lmx077
- "3" Rabin-karp string search algorithm-Pixiu
- "4" Source file src/pkg/strings/strings.go-the Go programming Language
Original link: Golang source code Analysis--String search algorithm, reproduced please indicate the source!