[prev] 56 [next]

Pattern Matching With Suffix Tries (cont)

suffixTrieMatch(trie,P):
|  Input  compact suffix trie for text T, pattern P of length m
|  Output starting index of a substring of T equal to P
|         -1 if no such substring exists
|
|  j=0, v=root of trie
|  repeat
|  |  // we have matched j characters
|  |  if ∃w∈children(v) such that P[j]=T[start(w)] then
|  |  |  i=start(w)            // start(w) is the start index of w
|  |  |  x=end(w)-i+1          // end(w) is the end index of w
|  |  |  if m≤x then   // length of suffix ≤ length of the node label?
|  |  |     if P[j..j+m-1]=T[i..i+m-1] then
|  |  |        return i-j      // match at i-j
|  |  |     else
|  |  |        return -1       // no match
|  |  |  else if P[j..j+x-1]=T[i..i+x-1] then
|  |  |     j=j+x, m=m-x       // update suffix start index and length
|  |  |     v=w                // move down one level
|  |  |  else return -1        // no match
|  |  |  end if
|  |  else
|  |     return -1
|  |  end if
|  until v is leaf node
|  return -1                   // no match