Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The failure function actually tells us this: if you matched X characters of a string, what is the longest suffix of such string such that it's also a prefix of a search string. </p> <p>You are asking how it's built, the approach is quite straightforward.</p> <p>If you add a new character at the end of a string, that is you are building f[x], and if it matches with character at position f[x-1], then f[x] is simply f[x-1]+1.</p> <p>In the other cases where it doesn't match, you try to find smaller and smaller suffixes and check if they match.</p> <p>For example, you have a word <code>"accadaccac"</code> for which you are building a failure function and you just added the letter <code>'c'</code>. Let's say you are building a failure function for the last letter, the letter <code>'c'</code>.</p> <ul> <li>First you check the failure function of the previous letter, its failure function was 4 because you can match suffix <code>"acca"</code> with the prefix <code>"acca"</code>, and now you add the letter <code>'c'</code>, it doesn't match with the letter <code>'d'</code> succeeding prefix <code>"acca"</code>.</li> <li>So you backtrack, to the last good suffix. You are now searching for a suffix of <code>"acca"</code> which is also a prefix of <code>"accadaccac"</code>, but is smaller than "acca". The answer to that question is f[length("acca")-1], or f[3], which is f[3] = 1, because suffix of length 1 (just the letter <code>'a'</code>) is also a prefix of a search string.</li> <li>And now you can try if the <code>'c'</code> matches with the character on the position 1, and voila, it matches, so now you know f[9] = f[f[8]-1]+1 = 2.</li> </ul> <p>I hope this will help you. Good luck! :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload