URAL 1297 Palindrome 后缀数组

时间:2023-03-09 01:46:38
URAL 1297 Palindrome   后缀数组
D - Palindrome

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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.
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
#include<iostream>
#include<string.h>
#include<ctype.h>
#include<cstdio>
#include<algorithm>
using namespace std;
#define Max 100020
char s[Max];
int sa[Max], Rank[Max], height[Max];
int wa[Max], wb[Max], wd[Max]; void build_sa(int n, int m){ // 倍增算法 n为总长度,n=l+1, m为字符范围
int i,j,p,*xy, *x = wa, *y = wb;
for(i = ; i < m; i ++) wd[i] = ;
for(i = ; i < n; i ++) wd[x[i]=s[i]] ++;
for(i = ; i < m; i ++) wd[i] += wd[i-];
for(i = n-; i >= ; i --) sa[-- wd[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 < m; i ++) wd[i] = ;
for(i = ; i < n; i ++) wd[x[y[i]]] ++;
for(i = ; i < m; i ++) wd[i] += wd[i-];
for(i = n-; i >= ; i --) sa[-- wd[x[y[i]]]] = y[i];
xy=x;x=y;y=xy;
p=;x[sa[]]=;
for(i=;i<n;i++){
x[sa[i]] = (y[sa[i-]]==y[sa[i]]&&y[sa[i-]+j]==y[sa[i]+j])?p-:p++;
}
}
} void getHeight(int n){ // 求height数组。
int i, j, k = ;
for(i = ; i <= n; i ++) Rank[sa[i]] = i;
for(i=;i<n ;height[Rank[i++]]=k)
for(k?k--:,j=sa[Rank[i]-];s[i+k]==s[j+k];k++);
}
//sa[1~~l]为有效值 sa[i]=a则代表排在第 i 位的是第a个后缀。 a属于[0~~l-1] 字符串比较不论长度
//Rank[0~~l-1]是有效值 Rank[i]=b则代表第 i 个后缀排在第b位 b属于[1~~l]
//height[2~~l]是有效值 height[i]=c 则代表排在第 i 位的后缀和排在第i-1的后缀的最长前缀长度是c
int strl=;
int max1(int ans,int num,int i){
if(num>ans) ans=num,strl=i;
return ans;
}
int main(){
int l,i,ans,T,temp,j;
while(scanf("%s",s)!=EOF){
l=strlen(s);
int oldl=l;
s[l]='|';
for(i=l+,j=l-;i<=l+l;i++,j--){
s[i]=s[j];
}
s[i]='\0';
l=*l+;
build_sa(l+,);
getHeight(l);
strl=;
ans=;
for(i=;i<=l;i++){
int a = sa[i-], b = sa[i];
if(a > b)
swap(a, b);
if(a < oldl && b > oldl && a+height[i] == l-b)
{
if(height[i] > ans)
ans = height[i], strl = a;
else if(height[i] == ans && a < strl)
strl = a;
}
}
for(i=strl;i<strl+ans;i++)
printf("%c",s[i]);
cout<<endl; }
return ;
}
/*
xatag,ata
*/