Если вы копируете списки в массив, может пригодиться следующее: Поскольку мы рассматриваем только палиндромы четной длины, я предполагаю, что это так. Но техника может быть легко расширена для работы с палиндромами странной длины.
Мы храним не фактическую длину палиндрома, а половину длины, поэтому мы знаем, сколько символов мы можем пройти влево / вправо.
Рассмотрим слово: 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
Моя точка зрения такова: как только мы найдем палиндромы, мы сможем отразить (хотя бы часть) таблицы длины палиндрома и, таким образом, вывести информацию о новых персонажах.
Таким образом, мы можем сохранить сравнения.