Knuth–Morris–Pratt algorithm: Difference between revisions

Content deleted Content added
Avhohlov (talk | contribs)
m Variable i renamed to k (for next edit)
Avhohlov (talk | contribs)
Variable m (beginning of current match in S) changed to j = m + k (current position in S)
Line 134:
'''define variables''':
an integer, mj ← 0 (the beginningposition of the current matchcharacter in S)
an integer, k ← 0 (the position of the current character in W)
an array of integers, T (the table, computed elsewhere)
Line 140:
'''let''' nP ← 0
'''while''' m + kj < length(S) '''do'''
'''if''' W[k] = S[m + kj] '''then'''
'''let''' j ← j + 1
'''let''' k ← k + 1
'''if''' k = length(W) '''then'''
(occurrence found, if only first occurrence is needed, m may be returned at this point)
'''let''' P[nP] ← mj - k, nP ← nP + 1
'''let''' m ← m + k - T[k], k ← T[k] (T[length(W)] can't be -1)
'''else'''
'''let''' k ← k + 1
'''else'''
'''iflet''' k ← T[k] > -1 '''then'''
'''letif''' m ← m + k -< T[k],0 k ← T[k]'''then'''
'''elselet''' j ← j + 1
'''let''' mk m + k + 1, k ← 0
 
===Efficiency of the search algorithm===