ural 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

题目大意:

给你一个字符串,求出其中最长回文串。

首先将这个字符串后加一个'0’在把这个字符串反向再存一遍。

然后对于以每一个点为中心的回文串,我们分两种情况:

长度为奇数,求i,n-i-1的最长公共前缀。

长度为偶数,求i,n-i的最长公共前缀。(简称lcp)

不断枚举中心点,比较答案即可。

至于求lcp,我们只需要先求出后缀数组中的h数组,做成st表查找rk[i],rk[j]中的最小值即可。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=2005;
char sr[N];
int sa[N],wa[N<<1],wb[N<<1],to[N],f[13][N];
int ask(int l,int r)
{
    int k=0;
    for(;1<<(k+1)<=r-l+1;++k);
    return min(f[k][l],f[k][r-(1<<k)+1]);
}
int main()
{
    scanf("%s",sr);
    int l=strlen(sr),n=2*l+1,m=128,*x=wa,*y=wb,mx=0,wz;
    sr[l]='0';
    for(int i=l+1;i<n;++i) sr[i]=sr[2*l-i];
    for(int i=0;i<n;++i) ++to[x[i]=sr[i]];
    for(int i=1;i<m;++i) to[i]+=to[i-1];
    for(int i=n-1;i>=0;--i) sa[--to[x[i]]]=i;
    for(int j=1,p=0;p<n;m=p,j<<=1)
    {
        p=0;
        for(int i=n-j;i<n;++i) y[p++]=i,x[i+j]=-1;;
        for(int i=0;p<n;++i) if(sa[i]>=j) y[p++]=sa[i]-j;
        for(int i=0;i<m;++i) to[i]=0;
        for(int i=0;i<n;++i) ++to[x[y[i]]];
        for(int i=1;i<m;++i) to[i]+=to[i-1];
        for(int i=n-1;i>=0;--i) sa[--to[x[y[i]]]]=y[i];
        int *t=x;x=y;y=t;
        x[sa[0]]=0;
        for(int i=p=1;i<n;++i)
            y[sa[i]]!=y[sa[i-1]]||y[sa[i]+j]!=y[sa[i-1]+j]?x[sa[i]]=p++:x[sa[i]]=p-1;
    }
    for(int i=0,j,k=0;i<n;f[0][x[i++]]=k)
        if(!x[i]) k=0;
        else for(k?--k:0,j=sa[x[i]-1];i+k<n&&j+k<n&&sr[i+k]==sr[j+k];++k);
    for(int i=1;(1<<i)<=n;++i)
        for(int j=1;j+(1<<i)-1<=n;++j)
            f[i][j]=min(f[i-1][j],f[i-1][j+(1<<i-1)]);
    for(int i=0;i<l;++i)
    {
        int l=x[i],r=x[n-i-1];
        if(l>r) swap(l,r);
        int t=ask(l+1,r)*2-1;
        if(t>mx) mx=t,wz=i;
        if(i)
        {
            l=x[i];r=x[n-i];
            if(l>r) swap(l,r);
            t=ask(l+1,r)*2;
            if(t>mx) mx=t,wz=i;
        }
    }
    for(int i=wz-mx/2;i<=wz+mx/2-!(mx&1);++i)
        printf("%c",sr[i]);
    return 0;
}

 

转载于:https://www.cnblogs.com/bzmd/p/6391212.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值