URAL 1297 palindrome suffix array

Source: Internet
Author: User

D-palindromeTime limit:1000MS Memory Limit:65536KB 64bit IO Format:%i64d &%i6 4u Submit Status Practice URAL 1297

Description

The "U.S. Robots" HQ has just received a rather alarming anonymous letter. It states the agent from the Competing«robots Unlimited»has 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, the 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

input Output
Thesampletextthatcouldbereadedthesameinbothordersarozaupalanalapuazora
Arozaupalanalapuazora
#include <iostream>#include<string.h>#include<ctype.h>#include<cstdio>#include<algorithm>using namespacestd;#defineMax 100020CharS[max];intSa[max], Rank[max], Height[max];intWa[max], Wb[max], Wd[max];voidBuild_sa (intNintm) {//multiplication algorithm n for total length, n=l+1, M for character range    intI,j,p,*xy, *x = wa, *y =WB;  for(i =0; I < m; i + +) wd[i] =0;  for(i =0; I < n; i + +) Wd[x[i]=s[i]] + +;  for(i =1; I < m; i + +) Wd[i] + = wd[i-1];  for(i = n1; I >=0; I--) sa[--wd[x[i]] =i;  for(j=1, p=1;p <n;j*=2, m=p) {         for(p=0, i = n-j; I < n; i + +) y[p++] =i;  for(i =0; I < n; i + +)if(Sa[i] >= j) Y[p + +] = Sa[i]-J;  for(i =0; I < m; i + +) wd[i] =0;  for(i =0; I < n; i + +) Wd[x[y[i]] + +;  for(i =1; I < m; i + +) Wd[i] + = wd[i-1];  for(i = n1; I >=0; I--) sa[--wd[x[y[i]] [=Y[i]; XY=x;x=y;y=XY; 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])? p1:p + +; }    }}voidGetHeight (intN) {//The array of height is evaluated.     intI, j, k =0;  for(i =1; I <= N; i + +) rank[sa[i]] =i;  for(i=0; i<n; height[rank[i++]]=.k) for(k?k--:0, j=sa[rank[i]-1];s[i+k]==s[j+k];k++);}//Sa[1~~l] is a valid value sa[i]=a is a suffix that is ranked in the first bit. A belongs to [0~~L-1] string comparisons regardless of length//RANK[0~~L-1] is a valid value rank[i]=b the first suffix is ranked b bit B belongs to [1~~l]//Height[2~~l] is a valid value height[i]=c is the suffix that is ranked in the first and the longest prefix length of the suffix i-1 is CintStrl=0;intMAX1 (intAnsintNuminti) {    if(Num>ans) ans=num,strl=i; returnans;}intMain () {intl,i,ans,t,temp,j;  while(SCANF ("%s", s)! =EOF) {L=strlen (s); intOldl=M; S[L]='|';  for(i=l+1, j=l-1; i<=l+l;i++,j--) {S[i]=S[j]; } S[i]=' /'; L=2*l+1; Build_sa (L+1, $);        GetHeight (l); Strl=0; Ans=1;  for(i=2; i<=l;i++){            intA = sa[i-1], B =Sa[i]; if(A >b) Swap (A, b); if(A < OLDL && B > Oldl && a+height[i] = = Lb) {if(Height[i] >ans) ans= Height[i], Strl =A; Else if(Height[i] = = ans && a <Strl) Strl=A; }        }         for(i=strl;i<strl+ans;i++) printf ("%c", S[i]); cout<<Endl; }    return 0;}
/*
Xatag,ata
*/

URAL 1297 palindrome suffix array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.