第一步,根据模板字符串P,构建匹配表T,其中P的字符数为 n,T的size也为 n:
- Set T[ 0] = 0, and let P have n characters.
- Set i = 1, j = 0.
- If i >= n then we are done; terminate the algorithm. Otherwise, compare P[i] with P[j].
- If they are equal, set T[i] = j + 1, j = j + 1, and i = i + 1.
- If not, and if j > 0, set j = T[j − 1].
- Otherwise, set T[i] = 0, i = i + 1.
- Return to step 3.
- Let i = m = 0; say P has n characters, and S, l characters.
- If m + i = l, then we exit; there is no match. Otherwise, compare P[i] versus S[m + i].
- If they are equal, set i = i + 1. If i = n then we have found a full match; terminate the algorithm and return m as the start of the match.
- If they are unequal, let e = T[i − 1]. Set m = m + i − e and if i > 0, set i = e.
- Return to step 2.
没有评论:
发表评论