Description
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
input | output |
---|---|
ThesampletextthatcouldbereadedthesameinbothordersArozaupalanalapuazorA | ArozaupalanalapuazorA |
/*
URAL 1297 Palindrome
题意:给定一个字符串,求最长回文子串.
类型:后缀数组
分析:首先用将该字符串本身反转之后与自己连接起来,中间用一个未出现的字符隔开,求前一段和后一段的最长公共前缀
第一种方法比较好想,是枚举前面的每一位,和后面一段的每一位求最长公共前缀,不过看了论文发现这并不是最优的方法.
第二种方法是论文里说的方法:枚举回文串的中间字符,查询对应的映射出来的位置和自己的最长公共前缀来找到以这个字符为中心的回文串的长度最大值
比如字符串97 97 98 97,最长的是97 98 97:
0 1 2 3 4 5 6 7 8 9
97 97 98 97 1 97 98 97 97 0 (1是用来分隔的字符,0是末尾补的可以无视)
0位映射在8, 1位映射在7, 2位映射在6, 3位映射在5
1.回文长度是偶数,我们以当前位和当前的前一位组成偶数长度,例如枚举i=1时(97),
它的前一位0(97)能与它组成长度是偶数的回文,而0位映射在8,所以等价于求以1为后缀和以8为后缀的最长公共前缀
即{97 98 97 1 97 98 97 97}、{97},得到的结果*2就是回文长度,因为上面的串是以1分隔对称,所以映射回来的样子是:
------ -----------
0 1 | | 1 2 3
97 97| | 97 98 97
<----- 后缀方向 --------->
2.回文长度是奇数,我们以当前位和当前的映射求最长公共前缀,例如枚举i=2时(98),
与i=6(98)求最长公共前缀{98 97 1 97 98 97 97}、{98 97 97}长度为2,得到的结果*2-1就是回文长度
为什么呢?我们把它映射回来看看就好理解了:
--------- --------
0 1 2 | | 2 3
97 97 98| | 98 97
<------- 后缀方向------->
理解了这个之后,要求解最长公共前缀的问题,其实就是求两个后缀排名之间的height的最小值,
加个RMQ求区间最小值就可以解决这个问题
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int MAXN=12010;
int sa[MAXN];
int rank[MAXN];
int height[MAXN];
int t1[MAXN],t2[MAXN],c[MAXN];
int s[MAXN];
int Min[MAXN][32];
int a[MAXN];
void RMQ_init(int n)
{
for(int i=1;i<=n;i++)Min[i][0]=a[i];
for(int j=1;(1<<j)<=n;j++)
for(int i=1;i+(1<<j)-1<=n;i++)
Min[i][j]=min(Min[i][j-1],Min[i+(1<<(j-1))][j-1]);
}
int RMQ(int l,int r)
{
int k=0;
while((1<<(k+1))<=r-l+1)k++;
return min(Min[l][k],Min[r-(1<<k)+1][k]);
}
void build_sa(int s[],int n,int m) //得到SA数组
{
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]=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(j=1;j<=n;j<<=1){
p=0;
for(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++)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]]=y[sa[i-1]]==y[sa[i]] && y[sa[i-1]+j]==y[sa[i]+j]?p-1:p++;
if(p>=n)break;
m=p;
}
}
void getHeight(int s[],int n){ //得到height数组
int i,j,k=0;
for(i=0;i<=n;i++)rank[sa[i]]=i;
for(i=0;i<n;i++){
if(k)k--;
j=sa[rank[i]-1];
while(s[i+k]==s[j+k])k++;
height[rank[i]]=k;
}
}
char ss[MAXN];
int main()
{
//freopen("F:\\input.txt","r",stdin);
int n,Max=-1;
scanf("%s",ss);
int len=strlen(ss);
for(int i=0;i<len;i++){
s[i]=ss[i];
if(ss[i]>Max)Max=ss[i];
}
s[len]=1;
for(int i=len-1,j=len+1;i>=0;i--,j++){
s[j]=ss[i];
}
n=2*len+1;
s[n]=0;
//for(int i=0;i<n;i++)printf("%d ",s[i]);
build_sa(s,n+1,128);
getHeight(s,n);
for(int i=2;i<=n;i++)a[i]=height[i];
RMQ_init(n);
int tmp=0,res=0,begi=0,left,right;
for(int i=0;i<len;i++){
//偶数长度
left=min(rank[i],rank[n-i]);
right=max(rank[i],rank[n-i]);
tmp=RMQ(left+1,right);
if(2*tmp>res){
res=2*tmp;
begi=i-res/2;
}
//奇数长度
left=min(rank[i],rank[n-i-1]);
right=max(rank[i],rank[n-i-1]);
tmp=RMQ(left+1,right);
if(2*tmp-1>res){
res=2*tmp-1;
begi=i-res/2;
}
}
ss[begi+res]=0;
printf("%s\n",ss+begi);
return 0;
}