リストを配列にコピーする場合、次のことが役立つ可能性があります。偶数の長さの回文のみを考慮するため、この場合を想定しています。しかし、この手法は、奇数の長さのパリンドロームで機能するように簡単に拡張できます。
回文の実際の長さではなく、半分の長さを保存するので、左右に何文字移動できるかがわかります。
単語を考えてみましょう:aabbabbabab
。最長の回文を探しています。
a a b b a b b a b a b (spaces for readability)
°^° start at this position and look to the left/right as long as possible,
1 we find a palindrome of length 2 (but we store "1")
we now have a mismatch so we move the pointer one step further
a a b b a b b a b a b
^ we see that there's no palindrome at this position,
1 0 so we store "0", and move the pointer
a a b b a b b a b a b
° °^° ° we have a palindrome of length 4,
1 0 2 so we store "2"
naively, we would move the pointer one step to the right,
but we know that the two letters before pointer were *no*
palindrome. This means, the two letters after pointer are
*no* palindrome as well. Thus, we can skip this position
a a b b a b b a b a b
^ we skipped a position, since we know that there is no palindrome
1 0 2 0 0 we find no palindrome at this position, so we set "0" and move on
a a b b a b b a b a b
° ° °^° ° ° finding a palindrome of length 6,
1 0 2 0 0 3 0 0 we store "3" and "mirror" the palindrome-length-table
a a b b a b b a b a b
^ due to the fact that the previous two positions hold "0",
1 0 2 0 0 3 0 0 0 we can skip 2 pointer-positions and update the table
a a b b a b b a b a b
^ now, we are done
1 0 2 0 0 3 0 0 0 0
つまり、回文の位置が見つかるとすぐに、テーブルの一部を推測できます。
もう一つの例:aaaaaab
a a a a a a b
°^°
1
a a a a a a b
° °^° °
1 2 1 we can fill in the new "1" since we found a palindrome, thus mirroring the
palindrome-length-table
a a A A a a b (capitals are just for emphasis)
^ at this point, we already know that there *must* be a palindrome of length
1 2 1 at least 1, so we don't compare the two marked A's!, but start at the two
lower-case a's
私のポイントは、パリンドロームを見つけるとすぐに、パリンドロームの長さのテーブル(少なくとも一部)をミラーリングして、新しいキャラクターに関する情報を推測できる可能性があるということです。このようにして、比較を保存できます。