Palindrome
Time limit:1000MSMemory limit:16384KB64bit Io format:% I64d & % i64u
Submit Status Appoint description:
System crawler)
Description
The "U. s. robots "HQ has just initialized ed 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 wowould 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. obviusly, 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 cocould finish that module, so the descrambler will choose the text scanning ction 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.
In addition, it is reasonable to assume that the agent will be sending a very long message, so John has simply to find the longest message satisfying the mentioned property. your task is to help John Pupkin by writing a program to find the secret message in the text of a given article. as nprx8086 ignores white spaces and punctuation marks, John will remove them from the text before feeding it into the program.
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 shoshould output the first of them.
Sample Input
Input |
Thesampletextthatcouldbereadedthesameinbothordersarozaupalanalapuazora |
Output |
Arozaupalanalapuazora |
As an entry question for the suffix array, but finding the longest reply substring is fasterAlgorithm.
// ========================================================== ========================================== // Name: Ural. cpp // Author: // Version: // Copyright: Your copyright notice // Description: Hello world in C ++, ANSI-style // ========================================================== ========================================== # Include <Iostream> # Include < String . H> # Include <Algorithm> # Include <Stdio. h> Using Namespace STD; Const Int Maxn = 2010 ; /* * Suffix array * Multiplication Algorithm O (N * logn) * The length of the array to be sorted is N, which is placed between 0 and 0 ~ In n-1, add 0 * build_sa (, n + 1,) at the end; // note that it is n + 1; * getheight (, n); * For example: * n = 8; * num [] = {1, 1, 2, 1, 1, 1, 1, 2, $}; note that the last digit of num is 0, other values greater than 0 * Rank [] = {4, 6, 8, 1, 2, 3, 5, 7, 0}; rank [0 ~ N-1] is a valid value. Rank [N] Must be 0. The value * Sa [] = {8, 3, 4, 5, 0, 6, 1, 7, 2} is invalid }; sa [1 ~ N] is a valid value, sa [0] Must be n is an invalid value * Height [] = {0, 0, 3, 2, 3, 1, 2, 0, 1 }; height [2 ~ N] is a valid value * */ Int Sa [maxn]; // Sa array, indicating to sort the N suffixes of S in ascending order // The starting position of the suffix is sequentially placed in SA. Int T1 [maxn], T2 [maxn], C [maxn]; // Evaluate the intermediate variable required by the SA array without assigning a value Int Rank [maxn], height [maxn]; // The string to be sorted is placed in the S array, from S [0] To s [n-1]. The length is n and the maximum value is less than M, // All s [I] Except S [n-1] are greater than 0, R [n-1] = 0 // After the function is completed, the result is placed in the SA array. Void Build_sa ( Int S [], Int N, Int M ){ Int I, j, P, * x = T1, * Y = T2; // In the first round of base sorting, if the maximum value of S is large, you can change it to quick sorting. For (I = 0 ; I <m; I ++) C [I] = 0 ; For (I = 0 ; I <n; I ++) C [x [I] = s [I] ++ ; For (I = 1 ; I <m; I ++) C [I] + = C [I- 1 ]; For (I = N- 1 ; I> = 0 ; I --) SA [-- C [x [I] = I; For (J = 1 ; J <= N; j <= 1 ) {P = 0 ; // Sort the second keyword directly using the SA Array For (I = N-J; I <n; I ++) y [p ++] = I; // The minimum number of J numbers after which the second keyword is null For (I = 0 ; I <n; I ++) If (SA [I]> = J) y [p ++] = sa [I]- J; // In this way, array y stores the result sorted by the second keyword. // First keyword of base sort For (I = 0 ; I <m; I ++) C [I] = 0 ; For (I = 0 ; I <n; I ++) C [x [Y [I] ++ ; For (I = 1 ; I <m; I ++) C [I] + = C [I- 1 ]; For (I = N-1 ; I> = 0 ; I --) SA [-- C [x [Y [I] = Y [I]; // Calculate the new array X based on the SA and array x Swap (x, y); P = 1 ; X [SA [ 0 ] = 0 ; For (I = 1 ; I <n; I ++ ) X [SA [I] = Y [SA [I- 1 ] = Y [SA [I] & Y [SA [I- 1 ] + J] = Y [SA [I] + J]? P- 1 : P ++ ; If (P> = N) Break ; M = P; // Maximum Value of next base sort }} Void Getheight ( Int S [], Int N ){ Int I, J, K =0 ; For (I = 0 ; I <= N; I ++) rank [SA [I] = I; For (I = 0 ; I <n; I ++ ){ If (K) k -- ; J = Sa [rank [I]- 1 ]; While (S [I + k] = s [J + k]) K ++ ; Height [rank [I] =K ;}} Int Rmq [maxn]; Int Mm [maxn]; Int Best [ 20 ] [Maxn]; Void Initrmq ( Int N) {mm [ 0 ] =- 1 ; For ( Int I =1 ; I <= N; I ++ ) Mm [I] = (I & (I- 1 ) = 0 )? Mm [I- 1 ] + 1 : Mm [I- 1 ]; For ( Int I = 1 ; I <= N; I ++) Best [ 0 ] [I] = I; For (Int I = 1 ; I <= mm [N]; I ++ ) For ( Int J = 1 ; J + ( 1 <I )- 1 <= N; j ++ ){ Int A = Best [I- 1 ] [J]; Int B = Best [I- 1 ] [J + ( 1 <(I- 1 )]; If (Rmq [a] <rmq [B]) Best [I] [J] = A; Else Best [I] [J] = B ;}} Int Askrmq ( Int A, Int B ){ Int T; t = Mm [B-A + 1 ]; B -= ( 1 <T )- 1 ; = Best [T] [a]; B = Best [T] [B]; Return Rmq [a] <rmq [B]? A: B ;} Int LCP ( Int A, Int B) { = Rank [a]; B = Rank [B]; If (A> B) Swap (A, B ); Return Height [askrmq (a + 1 , B)];} Int R [maxn]; Char STR [maxn]; Int Main (){ // Freopen ("in.txt", "r", stdin ); // Freopen ("out.txt", "W", stdout ); While (Scanf ( " % S " , & Str) = 1 ){ Int Len = Strlen (STR ); Int N = 2 * Len + 1 ; For ( Int I = 0 ; I <Len; I ++) R [I] = STR [I]; R [Len] = 1 ;// This should be different from others. For ( Int I = 0 ; I <Len; I ++) R [I + Len + 1 ] = STR [Len- 1 - I]; R [N] = 0 ; Build_sa (R, n + 1 , 128 ); Getheight (R, N ); For (Int I = 1 ; I <= N; I ++) rmq [I] = Height [I]; initrmq (N ); Int Ans = 0 , St; Int TMP; For ( Int I = 0 ; I <Len; I ++ ) {TMP = LCP (I, n-I ); // Even Symmetry If ( 2 * TMP> Ans) {ans = 2 * TMP; ST = I- TMP;} TMP = LCP (I, n-I- 1 ); // Odd symmetric If ( 2 * TMP- 1 > Ans) {ans = 2 * TMP- 1 ; ST = I-TMP + 1 ; }} STR [st + ANS] = 0 ; Printf ( " % S \ n " , STR + St );} Return 0 ;}