ural1297 . Palindrome

来源:互联网 发布:帝国cms英文语言包 编辑:程序博客网 时间:2024/05/21 10:50

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» 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.
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 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)
互缀数组的经典运用,求后缀数组,这里,我们连接两个字符串,中间用一个最大的值间隔,然后,求出height,这样就可以转化成了一个后缀的最长公共前缀,枚举,中间值,分奇偶,然后,就是一个rmq问题了,这样就好办了,用一个st算法,前的复杂度是st算法n*logn+0(1)+0(n);
#include <iostream>#include <stdio.h>#include <math.h>#include <string.h>using namespace std;#define maxn 200500int wa[maxn],wb[maxn],ws[maxn],wv[maxn],wsd[maxn],r[maxn],ans[maxn];char str[maxn];#define M 100010int dp[M][18];#define F(x) ((x)/3+((x)%3==1?0:tb))#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)int c0(int *r,int a,int b){return r[a]==r[b]&&r[a+1]==r[b+1]&&r[a+2]==r[b+2];}int c12(int k,int *r,int a,int b){if(k==2) return r[a]<r[b]||r[a]==r[b]&&c12(1,r,a+1,b+1);else return r[a]<r[b]||r[a]==r[b]&&wv[a+1]<wv[b+1];}void sort(int *r,int *a,int *b,int n,int m){    int i;    for(i=0;i<n;i++) wv[i]=r[a[i]];    for(i=0;i<m;i++) wsd[i]=0;    for(i=0;i<n;i++) wsd[wv[i]]++;    for(i=1;i<m;i++) wsd[i]+=wsd[i-1];    for(i=n-1;i>=0;i--) b[--wsd[wv[i]]]=a[i];    return;}void dc3(int *r,int *sa,int n,int m){    int i,j,*rn=r+n,*san=sa+n,ta=0,tb=(n+1)/3,tbc=0,p;    r[n]=r[n+1]=0;    for(i=0;i<n;i++) if(i%3!=0) wa[tbc++]=i;    sort(r+2,wa,wb,tbc,m);    sort(r+1,wb,wa,tbc,m);    sort(r,wa,wb,tbc,m);    for(p=1,rn[F(wb[0])]=0,i=1;i<tbc;i++)        rn[F(wb[i])]=c0(r,wb[i-1],wb[i])?p-1:p++;    if(p<tbc) dc3(rn,san,tbc,p);    else for(i=0;i<tbc;i++) san[rn[i]]=i;    for(i=0;i<tbc;i++) if(san[i]<tb) wb[ta++]=san[i]*3;    if(n%3==1) wb[ta++]=n-1;    sort(r,wb,wa,ta,m);    for(i=0;i<tbc;i++) wv[wb[i]=G(san[i])]=i;    for(i=0,j=0,p=0;i<ta && j<tbc;p++)        sa[p]=c12(wb[j]%3,r,wa[i],wb[j])?wa[i++]:wb[j++];    for(;i<ta;p++) sa[p]=wa[i++];    for(;j<tbc;p++) sa[p]=wb[j++];    return;}int cmp(int *r,int a,int b,int l){return r[a]==r[b]&&r[a+l]==r[b+l];}void da(int *r,int *sa,int n,int m){    int i,j,p,*x=wa,*y=wb,*t;    for(i=0;i<m;i++) wsd[i]=0;    for(i=0;i<n;i++) wsd[x[i]=r[i]]++;    for(i=1;i<m;i++) wsd[i]+=wsd[i-1];    for(i=n-1;i>=0;i--) sa[--wsd[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<n;i++) wv[i]=x[y[i]];        //м╟ее        for(i=0;i<m;i++) wsd[i]=0;        for(i=0;i<n;i++) wsd[wv[i]]++;        for(i=1;i<m;i++) wsd[i]+=wsd[i-1];        for(i=n-1;i>=0;i--) sa[--wsd[wv[i]]]=y[i];        //м╟ее        for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)            x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;    }    return;}int rank[maxn],height[maxn];void calheight(int *r,int *sa,int n){    int i,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];r[i+k]==r[j+k];k++);    return;}void makermq(int n,int b[]){    int i,j;    for(i=1;i<=n;i++)        dp[i][0]=b[i];    for(j=1;(1<<j)<=n;j++)        for(i=1;i+(1<<j)-1<=n;i++)            dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);}int rmq(int s,int v){    s=rank[s],v=rank[v];    if(s>v)swap(s,v);    s++;    int k=(int)(log((v-s+1)*1.0)/log(2.0));    return min(dp[s][k],dp[v-(1<<k)+1][k]);}int solve(int n,int l){    int i,tmp,sum=0,maxx=1,last=0,j;    for(i=0;i<l;i++){        tmp=rmq(i,n-i-1);        if(tmp*2-1>maxx){            maxx=tmp*2-1;            last=i-tmp+1;        }        tmp=rmq(i,n-i);        if(tmp*2>maxx){            maxx=tmp*2;            last=i-tmp;        }    }    for(i=last,j=0;j<maxx;j++)    printf("%c",str[i++]);    printf("\n");    return maxx;}int main(){    int n,n1,i,tcase;    while(scanf("%s",str)!=EOF){        n=strlen(str);        int l=n;        for( i=0;i<n;i++)        r[i]=str[i];        r[i++]=245;        for(int j=n-1;j>=0;j--){        r[i++]=str[j];        }        r[i++]=0;        n=2*n+1;        int m=300;        //da(r,ans,n+1,m);        dc3(r,ans,n+1,m);        calheight(r,ans,n);        makermq(n,height);        int maxx=solve(n,l);    }    return 0;}