Description
The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent from the competing ?Robots Unlimited? has infiltrated into “U.S. Robotics”. ?U.S. Robots? security service would have already started an undercover operation to establish the agent’s identity, but, fortunately, the letter describes communication channel the agent uses. He will publish articles containing stolen data to the “Solaris” almanac. Obviously, he will obfuscate the data, so “Robots Unlimited” will have to use a special descrambler (“Robots Unlimited” part number NPRx8086, specifications are kept secret). Having read the letter, the “U.S. Robots” president recalled having hired the “Robots Unlimited” ex-employee John Pupkin. President knows he can trust John, because John is still angry at being mistreated by “Robots Unlimited”. Unfortunately, he was fired just before his team has finished work on the NPRx8086 design. So, the president has assigned the task of agent’s message interception to John. At first, John felt rather embarrassed, because revealing the hidden message isn’t any easier than finding a needle in a haystack. However, after he struggled the problem for a while, he remembered that the design of NPRx8086 was still incomplete. “Robots Unlimited” fired John when he was working on a specific module, the text direction detector. Nobody else could finish that module, so the descrambler will choose the text scanning direction at random. To ensure the correct descrambling of the message by NPRx8086, agent must encode the information in such a way that the resulting secret message reads the same both forwards and backwards.Input
The input consists of a single line, which contains a string of Latin alphabet letters (no other characters will appear in the string). String length will not exceed 1000 characters.Output
The longest substring with mentioned property. If there are several such strings you should output the first of them.Sample Input
ThesampletextthatcouldbereadedthesameinbothordersArozaupalanalapuazorA
ArozaupalanalapuazorA
題意:求最長回文子串。
即RMQ(height, rank[j]+1, rank[k]),這是通過height[]數組的兩兩比較做到的
思路:窮舉每一位,然後計算以這個字符為中心的最長回文子串。注意這裡要分兩種情況,一是回文子串的長度為奇數,二是長度為偶數。兩種情況都可以轉化為求一個後綴和一個反過來寫的後綴的最長公共前綴。具體的做法是:將整個字符串反過來寫在原字符串後面,中間用一個特殊的字符隔開。這樣就把問題變為求這個新的字符串的某兩個後綴的最長公共前綴。然後在查找最長公共前綴lcp的時候利用到了RMQ,我們知道對於兩個後綴j和k,設rank[j]#include