1297. Palindrome
Time limit: 1.0 second
Memory limit: 64 MB
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.
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 | output |
---|---|
ThesampletextthatcouldbereadedthesameinbothordersArozaupalanalapuazorA |
ArozaupalanalapuazorA |
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
using namespace std;
#define N 2005
char a[N],bb[N];
int c[N],d[N],e[N],sa[N],height[N],n,b[N],m,dp[N][];
int cmp(int *r,int a,int b,int l)
{
return r[a]==r[b]&&r[a+l]==r[b+l];
}
void da()
{
int i,j,p,*x=c,*y=d,*t;
memset(b,,sizeof(b));
for(i=; i<n; i++)b[x[i]=a[i]]++;
for(i=; i<m; i++)b[i]+=b[i-];
for(i=n-; i>=; i--)sa[--b[x[i]]]=i;
for(j=,p=; p<n; j*=,m=p)
{
for(p=,i=n-j; i<n; i++)y[p++]=i;
for(i=; i<n; i++)if(sa[i]>=j)y[p++]=sa[i]-j;
for(i=; i<n; i++)e[i]=x[y[i]];
for(i=; i<m; i++)b[i]=;
for(i=; i<n; i++)b[e[i]]++;
for(i=; i<m; i++)b[i]+=b[i-];
for(i=n-; i>=; i--)sa[--b[e[i]]]=y[i];
for(t=x,x=y,y=t,p=,x[sa[]]=,i=; i<n; i++)
x[sa[i]]=cmp(y,sa[i-],sa[i],j)?p-:p++;
}
}
void callheight()
{
int i,j,k=;
b[]=;
for(i=; i<n; i++)b[sa[i]]=i;
for(i=; i<n-; height[b[i++]]=k)
for(k?k--:,j=sa[b[i]-]; a[i+k]==a[j+k]; k++);
}
int fun(int i,int j)
{
i=b[i];
j=b[j];
if(i>j)swap(i,j);
i++;
int k=(int)(log(j-i+1.0)/log (2.0));
return min(dp[i][k],dp[j-(<<k)+][k]);
}
void initrmq()
{
int i,j;
memset(dp,,sizeof(dp));
for(i=; i<=n; i++)
dp[i][]=height[i];
for(j=; (<<j)<=n; j++)
for(i=; i+(<<j)<=n; i++)
dp[i][j]=min(dp[i][j-],dp[i+(<<(j-))][j-]);
}
int main()
{
m=;
scanf("%s",a);
int size=n=strlen(a);
strcpy(bb,a);
a[n]='#';
strrev(bb);
strcat(a,bb);
n=strlen(a);
a[n++]='\0';
da();
callheight();
n--;
initrmq();
int maxa=,maxi=;
for(int i=; i<size; i++)
{
int tt=fun(i,n-i);
if((tt<<)>maxa)
{
maxa=tt<<;
maxi=i-tt;
}
tt=fun(i,n-i-);
if((tt<<)->maxa)
{
maxa=(tt<<)-;
maxi=i-(--tt);
}
}
for(int i=; i<maxa; i++)
putchar(a[i+maxi]);
putchar('\n');
}