Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your jump table constructing function simply does not check the needle for prefixes. We want to be able to look up, for each position in the needle, the length of the longest possible <em>proper prefix</em> of the needle leading up to (but not including) that position, other than the full prefix starting at <code>needle[0]</code> that just failed to match; this is how far we have to backtrack in finding the next match. Hence each entry in the jump table (say, <code>table[i]</code>) is exactly the length of the longest possible proper prefix of the needle which is also a prefix of the substring ending at <code>needle[i - 1]</code>.</p> <p>The first two entries in the jump table are -1 and 0, since a) a mismatch at the start of the pattern does not trigger backtracking (or, in other words, a prefix of zero length cannot have any proper prefixes or suffixes) and b) the empty string is considered to be of length 0.</p> <p><em>For more details please look at <a href="http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm" rel="nofollow">wikipedia</a> or an algorithms textbook.</em></p> <p>The code to accomplish the above is:</p> <pre><code>int *build_jump_table(const char * target) { if(!target) return NULL; int *table = new int[strlen(target) + 1]; if(!table) return NULL; table[0] = -1; /* unused by the matcher, just used here */ for(int i = 0; target[i] != '\0'; i++) { table[i+1] = table[i] + 1; while(table[i+1] &gt; 0 &amp;&amp; target[i] != target[table[i+1] - 1]) { table[i + 1] = table[table[i + 1] - 1] + 1; } } return table; } </code></pre> <p>which is quite verbose, and can be simplified a lot when you understand the concept behind the jump table.</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.
    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