ural1297 Palindrome 后缀数组

来源:互联网 发布:淘宝封号支付宝能用吗 编辑:程序博客网 时间:2024/05/21 06:33

1297. Palindrome

Time limit: 1.0 second
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.
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

inputoutput
ThesampletextthatcouldbereadedthesameinbothordersArozaupalanalapuazorA
ArozaupalanalapuazorA

题意:给定一个字符串,求最长回文子串。

思路:后缀数组。首先将字符串反转添加到后面,中间用一个字符串隔开,利用后缀数组求height数组,同时用RMQ预处理任意两个位置的最长公共前缀长度。

 枚举回文串的中心,分奇偶两种情况,对于偶数,若枚举中心为i,即查找位置为i-1在倒置后的位置,那么对应于反转字符串的查找位置为n-1-i;对于奇数,若枚举中心为i, 反转的查找位置为n-i-2,求最长公共前缀即可。详见代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int MAXN=2000+100;int n;char s[MAXN];int d[MAXN][20];int c[MAXN],sa[MAXN],rank[MAXN],height[MAXN],t1[MAXN],t2[MAXN];int cmp(int *r,int a,int b,int l){    return r[a] == r[b] && r[a+l] == r[b+l];}void build_sa(int m){    int i,k,p=0;    int *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(k=1;p<n;k<<=1,m=p){        for(p=0,i=n-k;i<n;i++) y[p++]=i;        for(i=0;i<n;i++) if(sa[i]>=k) y[p++]=sa[i]-k;        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);        for(p=1,x[sa[0]]=0,i=1;i<n;i++)            x[sa[i]]=cmp(y,sa[i-1],sa[i],k) ? p-1 : p++;     }}void  calheight(int n){    int k=0;    for(int i=1;i<=n;i++) rank[sa[i]]=i;    for(int i=0;i<n;i++){        if(k) k--;        int j=sa[rank[i]-1];        while(s[i+k] == s[j+k]) k++;        height[rank[i]]=k;    }}void RMQ_init(int n){    for(int i=1;i<=n;i++)        d[i][0]=height[i];    for(int j=1;(1<<j)<=n;j++)        for(int i=1;i+(1<<j)-1<=n;i++)             d[i][j]=min(d[i][j-1],d[i+(1<<(j-1))][j-1]); //i+(1<<(j-1)) 写成 i+1<<(j-1)就RE了。。}int lcp(int x,int y){    int l=rank[x],r=rank[y];    if(l>r) swap(l,r); l++;    int k=0;    while(1<<(k+1)<=r-l+1) k++;    return min(d[l][k],d[r-(1<<k)+1][k]);}int main(){    //freopen("text.txt","r",stdin);    while(~scanf("%s",s)){        int len=n=strlen(s);        s[n++]=1;        for(int i=len-1;i>=0;i--)            s[n++]=s[i];        s[n++]=0;        build_sa(200); calheight(n-1); RMQ_init(n-1);        int ans=0,w;        for(int i=0;i<len;i++){            int j=lcp(i,n-i-1);            if(ans<2*j) ans=2*j,w=i-j;            j=lcp(i,n-i-2);            if(ans<2*j-1) ans=2*j-1,w=i-j+1;        }        s[ans+w]=0;        printf("%s",s+w);    }}




0 0