1297.Palindrome
Time limit:1.0 Second
Memory limit:64 MB
The "U.S. Robots" HQ has just received a rather alarming anonymous letter. It states that the agent from the competing? Robots Unlimited? Have infiltrated into "U.S. robotics". ? U.S. Robots? Security Service would has already started an undercover operation to establish the agent's identity, but, fortunately, t He letter describes communication channel the agent uses. He'll publish articles containing stolen data to the "Solaris" Almanac. Obviously, he'll obfuscate the data, so "Robots Unlimited" would have to use a special Descrambler ("Robots Unlimited" pa RT number NPRx8086, specifications is kept secret).
Have read the letter, the "U.S. Robots" President recalled have 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 have finished work on the NPRx8086 design.
So, the president have 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 h Aystack. 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 is working on a specific module, the text direction detector. Nobody else could finish that module, so the Descrambler would choose the text scanning direction at random. To ensure the correct descrambling of the "the message by NPRx8086, agent must encode" information in such a "that the RE Sulting secret message reads the same both forwards and backwards.
In addition, it's reasonable to assume that the agent would be being sending a very long message, so John have 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 would remove them from the text before feeding it into the pro Gram.
Input
The input consists of a single line, which contains a string of Latin alphabet letters (no other characters would appear in The string). String length would not exceed characters.
Output
The longest substring with mentioned property. If There is several such strings you should output the first of them.
Sample
Input
Thesampletextthatcouldbereadedthesameinbothordersarozaupalanalapuazora
Output
Arozaupalanalapuazora
Problem Author:eugene Krokhalev
Problem Source:ix Open Collegiate programming Contest of the High School pupils (13.03.2004)
Test Instructions: Enter a string to find the longest palindrome string in its substring.
idea: suffix array application, you can write the string backwards to the original string and add a special character after the original string, so that you can enumerate the characters in the original string, Its suffix in the new string and the LCP (the longest common prefix) of its suffix in the reverse string is the palindrome portion length of the palindrome string. For LCP This RMQ problem takes the ST algorithm to preprocess.
This time the category discusses the Palindrome string length of odd and even two cases can be.
The time complexity of this procedure is determined by RMQ, which is O (Nlogn). If it is the manacher algorithm will be O (n).
[If the above description is not read]
The original string is set to ABB, which in turn becomes the BBA, the new string is "abb#bba0", then the "b#bba0" and "BA0" LCP is required (b| B
The original string is set to ABA, which in turn becomes ABA, the new string is "aba#aba0", then "ba#aba0" and "BA0" LCP is the desired (ABA)
The code is as follows:
/* * ID:J.SURE.1 * PROG: * lang:c++ * *#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <ctime>#include <cmath>#include <stack>#include <queue>#include <vector>#include <map>#include <set>#include <string>#include <iostream>#define PB push_back#define LL Long Longusing namespace STD;Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-8;/****************************************/Const intN =2e3+5, M = the;CharS[n];intSa[n], T[n], t2[n], c[n], rank[n], high[n];intdp[n][ +];BOOLcmpint(ynintIintK) {returny[sa[i-1] [= Y[sa[i]] && y[sa[i-1]+k] = = Y[sa[i]+k];}voidDaintNintm) {intI, *x = t, *y = t2; 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 = n1; I >=0; i--) Sa[--c[x[i]] = i; for(intK =1, p; K <= N; k<<=1, m=p) {p =0; for(i = n-k; i < n; i++) y[p++] = i; for(i =0; I < n; i++)if(Sa[i] >= k) y[p++] = sa[i]-K; 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 = n1; I >=0; i--) sa[--c[x[y[i]] [y[i]; Swap (x, y); p =1; x[sa[0]] =0; for(i =1; I < n; i++) {X[sa[i]] = cmp (y, I, k)? p1: p++; }if(P >= N) Break; }}voidGet_high (intN) {intK =0; for(inti =1; I <= N; i++) Rank[sa[i]] = i; for(inti =0; I < n; i++) {if(k) k--;intj = sa[rank[i]-1]; while(S[i+k] = = S[j+k]) {k++; } High[rank[i]] = k; }}voidRMQ (intN) { for(inti =1; I <= N; i++) {dp[i][0] = High[i]; } for(intj =1; (1<<J) <= N; J + +) { for(inti =1; i+ (1<<J)-1<= N; i++) {Dp[i][j] = min (dp[i][j-1], dp[i+ (1<< (J-1))][j-1]); } }}intLCP (intAintb) {intL = Rank[a], R = rank[b];if(L > R) Swap (L, R); l++;intK =Log(1.0* (r-l+1)) /Log(2.0);returnMin (Dp[l][k], dp[r-(1<<K) +1][k]);}voidSolveintNintLen) {intAns =1, idx =0; for(inti =0; i < Len; i++) {intOdd = LCP (i, n-i-1), even = LCP (i, n-i);if(2*odd-1> ans) {ans =2*odd-1; IDX = i-odd +1; }if(2*even > Ans) {ans =2*even; IDX = I-even; } } for(inti = idx; I < idx + ans; i++) {printf("%c", S[i]); }puts("");}intMain () {#ifdef j_sure //freopen ("000.in", "R", stdin); //freopen ("999.out", "w", stdout);#endif while(~scanf('%s ', s)) {intLen =strlen(s); S[len] =' # '; for(inti =1; I <= Len; i++) {S[len+i] = s[len-i]; }intn = len<<1|1; S[n] =0; Da (n+1+ j); Get_high (n); RMQ (n); Solve (n, Len); }return 0;}
"Suffix array | longest palindrome string" URAL-1297 palindrome