If you copy the lists into an array, the following may be useful: since we only consider even-palindromes, I assume this case. But the technique can be easily expanded to work with odd palindromes.
We save not the actual length of the palindrome, but half the length, so we know how many characters left / right we can go.
Consider the word: aabbabbabab . We are looking for the longest palindrome.
aabbabbabab (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 aabbabbabab ^ we see that there no palindrome at this position, 1 0 so we store "0", and move the pointer aabbabbabab ° °^° ° 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 aabbabbabab ^ 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 aabbabbabab ° ° °^° ° ° finding a palindrome of length 6, 1 0 2 0 0 3 0 0 we store "3" and "mirror" the palindrome-length-table aabbabbabab ^ 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 aabbabbabab ^ now, we are done 1 0 2 0 0 3 0 0 0 0
This means: As soon as we find the position of the palindrome, we can display some parts of the table.
Another example: aaaaaab
aaaaaab °^° 1 aaaaaab ° °^° ° 1 2 1 we can fill in the new "1" since we found a palindrome, thus mirroring the palindrome-length-table aa AA aab (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
My point is: as soon as we find the palindromes, we can mirror (at least part) the table of the length of the palindrome and thereby display information about new characters. That way we can keep comparisons.
phimuemue
source share