1. The regular string matching algorithm can match characters one by one from the beginning. In case of inequality, it can be traced back to continue matching one by one starting from the next position. The following is a program:
# Include
Using namespace std; # include
Int main () {int find (string, string); string s1 ("hello world"); string s2 ("world"); cout <
<
The matching process is as follows:
Assume that the length of the long string is n and the length of the matched string is m. The average complexity of the above algorithm is O (m * n). If there is a string, s1 = "aaaa... B ", there are first 100 a, s2 =" aaaaab ". When the above method is used for matching, it will be traced back 95 times, which is very inefficient, to improve string matching efficiency, you can use the KMP algorithm.
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink">
VcD4KPHA + IDwvcD4KPHA + kernel/rysfA + 9PD0tG + rbHIvc + z9rXEveG5 + kernel/Co7o8L3A + kernel/a1vbK7z + C1yLXE19a3 + kernel/kernel Release/release/gtcijrM/release + IDwvcD4KPHA + release + zb/release + vK19fWt/release/vT67 + qzbe1xstmbj219a3 + 7K7tcijqLvy 1d/release/CserOqmq1xNfWt/release + 9Prv6rNt7XEa7j219a3 + 8/release + release/w8L3A + release/K/release + CjxwPiA8L3A + release "brush: j Ava; "> void next (const char * s, int next []) {int j = 0, k =-1; next [0] =-1; while (s [j]! = '\ 0') {if (k =-1 | s [j] = s [k]) {+ j; ++ k; if (s [j]! = S [k]) next [j] = k; elsenext [j] = next [k];} elsek = next [k];} for (int I = 0; I
<<<< pre><<<<>
<