ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1354. Palindrome. Again Palindrome

how did use KMP algo to structure palindrom?
Posted by lian lian 26 Aug 2008 05:17

I think the question for some day, but I can`t solve....

please give hint.....

Edited by author 26.08.2008 05:22
Re: how did use KMP algo to structure palindrom?
Posted by Kolyanich 6 Feb 2011 21:36
First you can calculate prefix array for reversed word. And then run algorithm that finds occurence or reversed word in S1, but stop it when forward and backard iterators meet each other. Ignore requirement S2 to be nonempty for now, and take it into account when algorithm will be almost ready
Re: how did use KMP algo to structure palindrom?
Posted by Artem Khizha [DNU] 19 Jul 2011 16:27
Consider A - original string, B - reversed.
Build preffix array for B, run KMP (search for B in A) with slight fix. In usual KMP you find a number of letters, that coincide in strings, and want it to be equal to size of B. In this problem you just remember this number (let it be Q). So after KMP you know, that last Q letters are a palindrome.

The only moment is what needs to be done, if Q == size of A. Then you should undo last KMP step (Q = P[Q-1]) and find previous palindrome.