URAL 1297 后缀数组+RMQ求串的最长回文子串


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


首先把原始字符串倒转过来,然后接在原始字符串的后面,中间用一个不可能出现的字符隔开。枚举每一个原始字符串中的字符以它为中心(分为奇数和偶数两种情况)进行查找,比如对于下标为i的字符,当回文串为奇数时,我们要求的就是i的后缀与2*n-i

后缀的最长公共前缀了偶数是2*n-i+1。利用RMQheight数组,进而求LCP,函数如同多个子串的lcp函数


#include
#include
#include
#include
#define inf 0x3f3f3f3f
#include
using namespace std;
const int MAX=2020;
string str;
int n;
int wa[MAX],wb[MAX],wsf[MAX],wv[MAX],sa[MAX];
int rank[MAX],height[MAX],r[MAX];
int cmp(int *r,int a,int b,int k){return (r[a]==r[b])&&(r[a+k]==r[b+k]);
}
void da(int *r,int *sa,int n,int m)//此处N比输入的N要多1,为人工添加的一个字符,用于避免cmp时越界
{int i,j,p,*x=wa,*y=wb,*t;for(i=0;i=0;i--) sa[--wsf[x[i]]]=i;for(j=1,p=1;p=j) y[p++]=sa[i]-j;for(i=0;i=0;i--) sa[--wsf[wv[i]]]=y[i];for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;irank[r])return query(rank[r]+1,rank[l]);elsereturn query(rank[l]+1,rank[r]);
}
void print(int x,int lenx,int flag)
{if(flag==0)for(int i=x-(lenx-1)/2;i<=x+(lenx-1)/2;i++)cout<>str){int len=str.length();for(int i=0;i<=len-1;i++)r[i]=str[i];r[len]=129;for(int i=2*len;i>=len+1;i--)r[i]=str[2*len-i];n=2*len+1;r[n]=0;da(r,sa,n+1,150);calheight(r,sa,n);RMQ_init();int x1,x2;int lenx1=1,lenx2=0;for(int i=1;i<=len-1;i++){if(r[i]==129)continue;int j=2*len-i;if(lenx1<(2*lcp(i,j)-1)){lenx1=2*lcp(i,j)-1;x1=i;}j=2*len-i+1;if(lenx2<2*lcp(i,j)){lenx2=2*lcp(i,j);x2=i;}}if(max(lenx1,lenx2)==1){cout<x2)print(x2,lenx2,1);elseprint(x1,lenx1,0);}else{if(lenx1>lenx2)print(x1,lenx1,0);elseprint(x2,lenx2,1);}}return 0;
}




本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部