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.
Example
input
ThesampletextthatcouldbereadedthesameinbothordersArozaupalanalapuazorA
output
ArozaupalanalapuazorA
求最长回文子字符串,把原字符串倒着加入到句尾,两个字符串以额外字符隔开,把所有的位置当做中点遍历一遍,这样前半部分和倒置的后半部分都可以用后缀表示了,避免了两个子字符串放在同一父字符串和不对称的问题,判断height的大小即可,很巧妙的办法……注意偶数长度和奇数长度的起点不一样
#include <iostream>
#include <stdio.h>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <math.h>
#include <string.h>
using namespace std;
int mo[4][2]={0,1,1,0,0,-1,-1,0};
const int MAXN=0x3f3f3f3f;
const int sz=100005;
int n;
int t1[sz],t2[sz],c[sz],sa[sz],tsa[sz],rk[sz],height[sz],r[sz],RMQ[sz],mm[sz];
int best[20][sz];
char str[sz];
bool cmp(int *r,int a,int b,int l){
return r[a]==r[b]&&r[a+l]==r[b+l];
//hash,判断两个关键字是否完全一样
}
void da(int str[],int sa[],int rk[],int height[],int n,int m){
n++;
int i,j,p,*x=t1,*y=t2;
for(i=0;i<m;i++) c[i]=0;
for(i=0;i<n;i++) c[x[i]=str[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(j=1;j<=n;j<<=1){//排序的子字符串长度以2^n速度增长,所以只要排序logn次
p=0;
for(i=n-j;i<n;i++) y[p++]=i;
//后面j个数没有第二关键字,可以理解为第二关键字为0,排名放在最前
for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
//sa[i]-j的第二关键字是j,记录了第二关键字排名
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=n-1;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,sa[i-1],sa[i],j)?p-1:p++;
//这里的y是之前的x,记录数组编号对应的具体字符
//统计数字种类,缩小范围
if(p>=n) break;
//每个位置都单独对应了一个排名,可以不用排序了
m=p;
}
int k=0;
n--;
for(i=0;i<=n;i++) rk[sa[i]]=i;
for(i=0;i<n;i++){
if(k)k--;
j=sa[rk[i]-1];
while(str[i+k]==str[j+k])k++;
height[rk[i]]=k;
}
}
void initRMQ(int n){
mm[0]=-1;
int i,j;
for(i=1;i<=n;i++){
mm[i]=((i&(i-1))==0)?mm[i-1]+1:mm[i-1];
//当i是2^n时i&(i-1)为0,则此时查询的长度上限要增加一倍
}
for(i=1;i<=n;i++) best[0][i]=i;
for(i=1;i<=mm[n];i++){
for(j=1;j+(1<<i)-1<=n;j++){//j是起点,i是长度
int a=best[i-1][j];
int b=best[i-1][j+(1<<(i-1))];
if(RMQ[a]<RMQ[b]) best[i][j]=a;
else best[i][j]=b;
}
}
}
int askRMQ(int a,int b){
int t;
t=mm[b-a+1];
b-=(1<<t)-1;
a=best[t][a];b=best[t][b];
return RMQ[a]<RMQ[b]?a:b;
}
int lcp(int a,int b){
a=rk[a];b=rk[b];
if(a>b) swap(a,b);
return height[askRMQ(a+1,b)];
//a为什么+1?因为hight记录的是i和i-1之间的前缀长度,求a到b就要从a+1开始
}
int main()
{
int i,T;
//freopen("in.txt","r",stdin);
scanf("%d",&T);
while(T--){
scanf("%s",str);
int len=strlen(str);
n=len;
for(i=0;i<len;i++) r[i]=str[i];
r[len]=1;
r[n]=0;
da(r,sa,rk,height,n,128);
/*for(i=1;i<=n;i++) RMQ[i]=height[i];
initRMQ(n);*/
int ans=0;
/*for(i=1;i<=len;i++){
cout<<sa[i]<<' ';
}
cout<<endl;
for(i=1;i<=len;i++){
cout<<height[i]<<' ';
}
cout<<endl;
sa从0开始
rk从1开始
*/
for(i=1;i<=len;i++){
//printf("i:%d sa:%d h:%d\n",i,sa[i],height[i]);
ans+=len-sa[i]-height[i];
}
cout<<ans<<endl;
}
return 0;
}