1297. palindrome
Time limit:1.0 Second
Memory limit:64 MB
The "U.S. Robots" HQ has just received a rather alarminganonymous letter. It states the agent from the Competing«robotsunlimited»has infiltrated into "U.S. robotics". «u.s. Robots»security Servicewould have already started a undercover operation to establish the agent ' sidentity, but, fo Rtunately, the letter describes communication channel Theagent uses. He'll publish articles containing stolen data to the "Solaris" Almanac. Obviously, he'll obfuscate the data, so "Robots Unlimited" would haveto use a special Descrambler ("Robots Unlimited" par T number nprx8086,specifications is kept secret).
having read the letter, the "U.S. Robots" presidentrecalled having hired the "Robots Unlimited" Ex-employe E John Pupkin. Presidentknows He can trust John, because John is still angry at being mistreated by "Robots Unlimited". Unfortunately, he was fired just before his team hasfinished work on the NPRx8086 design.
so, the president have assigned the task of agent ' smessage interception to John. At first, John felt rather embarrassed, becauserevealing the hidden message isn ' t any easier than finding a needle in Ahay Stack. However, after he struggled the problem for a while, he remembered thatthe design of NPRx8086 was still incomplete. "Robots Unlimited" fired John Whenhe was working on a specific module, the text direction detector. Nobody elsecould finish that module, so the Descrambler would choose the text scanningdirection at random. To ensure the correct descrambling of the message byNPRx8086, agent must encode the information in such a. Ultingsecret message reads the same both forwards and backwards.
In addition, it's reasonable to assume that the agent would be sending a verylong message and so John have simply to find The longest message satisfying thementioned property.
Your task is to help John Pupkin by writing a program tofind the secret message in the text of a given article. As NPRx8086 ignoreswhite spaces and punctuation marks, John would remove them from the text beforefeeding it into the progr Am.
Input
The input consists of a, which contains astring of Latin alphabet letters (no other characters would appear in TheString). String length would not exceed characters.
Output
The longest substring with mentioned property. If Thereare Several such strings you should output the first of them.
Sample
Input |
Output |
Thesampletextthatcouldbereadedthesameinbothordersarozaupalanalapuazora |
Arozaupalanalapuazora |
Ideas
The longest palindrome string.
After the string is stitched together in the middle, separated by a character that does not appear, the problem is converted to the two specific suffix of the LCP, the enumeration of symmetric point I, the symmetric number is odd for the case of the LCP (i,n-i), the symmetric number of the case corresponding to the LCP (i,n-i-1).
:
The two-suffix LCP can be solved in O (Nlogn) time with the sparse table algorithm (multiplication).
Code
1#include <cstdio>2#include <cstring>3#include <iostream>4 #definefor (A,B,C) for (int a= (b); a<= (c); a++)5 using namespacestd;6 7 Const intMAXN = the+Ten; 8 Const intMaxd = A;9 Ten intS[MAXN]; One intSA[MAXN],C[MAXN],T[MAXN],T2[MAXN]; A - voidBuild_sa (intMintN) { - inti,*x=t,*y=T2; the 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(intk=1; k<=n;k<<=1) { + intp=0; A for(i=n-k;i<n;i++) y[p++]=i; at 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=0; i<m;i++) c[i]+=c[i-1]; - for(i=n-1; i>=0; i--) sa[--c[x[y[i]]]]=Y[i]; in - swap (x, y); top=1; x[sa[0]]=0; + for(i=1; i<n;i++) -x[sa[i]]=y[sa[i]]==y[sa[i-1]] && y[sa[i]+k]==y[sa[i-1]+k]?p-1:p + +; the if(p>=n) Break; *m=p; $ }Panax Notoginseng } - intRANK[MAXN],HEIGHT[MAXN]; the voidGetHeight (intN) { + inti,j,k=0; A for(i=0; i<=n;i++) rank[sa[i]]=i; the for(i=0; i<n;i++) { + if(k) k--; -j=sa[rank[i]-1]; $ while(S[j+k]==s[i+k]) k++; $height[rank[i]]=K; - } - } the intA[maxn][maxd]; - voidRmq_init (intN) {Wuyi for(intI=1; i<=n;i++) a[i-1][0]=Height[i]; the for(intk=1;(1<<K) <=n;k++) - for(intI=0;(i+ (1<<k)) <=n;i++) WuA[i][k]=min (a[i][k-1],a[i+ (1<< (K-1))][k-1]); - } About intQueryintLintr) { $ intk=0; - while(1<< (k +1) <= (r-l+1)) k++; - returnMin (a[l][k],a[r-(1<<K) +1][k]); - } A intLcpintAintb) { + intL=rank[a],r=Rank[b]; the if(r<l) swap (L,R); L--, r--; - if(r<0)return 0; $ returnQuery (L +1, R);//l+1 the } the the intN; the CharEXPR[MAXN]; - in intMain () { the while(SCANF ("%s", expr) = =1) { the intLen=strlen (expr), n=2*len+1; About for(intI=0; i<len;i++) s[i]=Expr[i]; thes[len]=1; the for(intI=0; i<len;i++) s[i+len+1]=expr[len-1-i]; thes[n]=0; + -Build_sa ('Z'+1, n+1); the getheight (n);Bayi rmq_init (n); the intans=0, front,tmp; the for(intI=0; i<n;i++) { -TMP=LCP (i,n-i-1); - if(2*tmp-1>ans) {//number of symmetries is odd theans=2*tmp-1; thefront=i-tmp+1; the } theTMP=LCP (i,n-i); - if(2*tmp>ans) {//number of symmetries is even theans=2*tmp; thefront=i-tmp; the }94 } theexpr[front+ans]=' /'; theprintf"%s\n", expr+front); the }98 return 0; About}
Ural1297 palindrome (suffix array)