Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I agree that the logic isn't quite right in the explanation of the link. I give some details below.</p> <p>Manacher's algorithm fills in a table P[i] which contains how far the palindrome centered at i extends. If P[5]=3, then three characters on either side of position five are part of the palindrome. The algorithm takes advantage of the fact that if you've found a long palindrome, you can fill in values of P on the right side of the palindrome quickly by looking at the values of P on the left side, since they should mostly be the same.</p> <p>I'll start by explaining the case you were talking about, and then I'll expand this answer as needed.</p> <p>R indicates the index of the right side of the palindrome centered at C. Here is the state at the place you indicated:</p> <pre><code>C=11 R=20 i=15 i'=7 P[i']=7 R-i=5 </code></pre> <p>and the logic is like this:</p> <pre><code>if P[i']&lt;=R-i: // not true else: // P[i] is at least 5, but may be greater </code></pre> <p>The pseudo-code in the link indicates that P[i] should be greater than or equal to P[i'] if the test fails, but I believe it should be greater than or equal to R-i, and the explanation backs that up.</p> <p>Since P[i'] is greater than R-i, the palindrome centered at i' extends past the palindrome centered at C. We know the palindrome centered at i will be at least R-i characters wide, because we still have symmetry up to that point, but we have to search explicitly beyond that.</p> <p>If P[i'] had been no greater than R-i, then the largest palindrome centered at i' is within the largest palindrome centered at C, so we would have known that P[i] couldn't be any larger than P[i']. If it was, we would have a contradiction. It would mean that we would be able to extend the palindrome centered at i beyond P[i'], but if we could, then we would also be able to extend the palindrome centered at i' due to the symmetry, but it was already supposed to be as large as possible.</p> <p>This case is illustrated previously:</p> <pre><code>C=11 R=20 i=13 i'=9 P[i']=1 R-i=7 </code></pre> <p>In this case, P[i']&lt;=R-i. Since we are still 7 characters away from the edge of the palindrome centered at C, we know that at least 7 characters around i are the same as the 7 characters around i'. Since there was only a one character palindrome around i', there is a one character palindrome around i as well.</p> <p>j_random_hacker noticed that the logic should be more like this:</p> <pre><code>if P[i']&lt;R-i then P[i]=P[i'] else if P[i']&gt;R-i then P[i]=R-i else P[i]=R-i + expansion </code></pre> <p>If P[i'] &lt; R-i, then we know that P[i]==P[i'], since we're still inside the palindrome centered at C.</p> <p>If P[i'] > R-i, then we know that P[i]==R-i, because otherwise the palindrome centered at C would have extended past R.</p> <p>So the expansion is really only necessary in the special case where P[i']==R-i, so we don't know if the palindrome at P[i] may be longer.</p> <p>This is handled in the actual code by setting P[i]=min(P[i'],R-i) and then always expanding. This way of doing it doesn't increase the time complexity, because if no expansion is necessary, the time taken to do the expansion is constant.</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