ural1297 Palindrome

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.

有效率更高的,复杂度O(n)的manacher算法【见这里】
把翻转串接在原串后面【中间用特殊字符隔开】,然后枚举回文串的中点,相当于询问两后缀的LCP【一个是原串的,一个是翻转串对应位置的】,注意分类讨论长度的奇偶。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char s[2010];
int sa[2010],rank[2010],height[2010],t1[2010],t2[2010],cnt[2010],f[10010][20];
int qry(int x,int y)
{
    int k=0;
    if (x>y) swap(x,y);
    x++;
    while ((1<<k+1)<=y-x+1) k++;
    return min(f[x][k],f[y-(1<<k)+1][k]);
}
int main()
{
    int i,j,k,l,m,n,p,q,*x=t1,*y=t2,ans=-1,id,kk,tem;
    scanf("%s",s+1);
    l=strlen(s+1);
    s[l+1]='$';
    for (i=l+2;i<=2*l+1;i++)
      s[i]=s[2*l-i+2];
    m='z';
    n=2*l+1;
    for (i=1;i<=n;i++)
      cnt[x[i]=s[i]]++;
    for (i=2;i<=m;i++)
      cnt[i]+=cnt[i-1];
    for (i=n;i;i--)
      sa[cnt[x[i]]--]=i;
    for (k=1;k<=n;k<<=1)
    {
        p=0;
        for (i=n-k+1;i<=n;i++)
          y[++p]=i;
        for (i=1;i<=n;i++)
          if (sa[i]-k>=1)
            y[++p]=sa[i]-k;
        for (i=1;i<=m;i++)
          cnt[i]=0;
        for (i=1;i<=n;i++)
          cnt[x[y[i]]]++;
        for (i=2;i<=m;i++)
          cnt[i]+=cnt[i-1];
        for (i=n;i;i--)
          sa[cnt[x[y[i]]]--]=y[i];
        swap(x,y);
        p=x[sa[1]]=1;
        for (i=2;i<=n;i++)
        {
            if (y[sa[i]]!=y[sa[i-1]]||y[sa[i]+k]!=y[sa[i-1]+k]) p++;
            x[sa[i]]=p;
        }
        if ((m=p)>=n) break;
    }
    for (i=1;i<=n;i++)
      rank[sa[i]]=i;
    for (i=1,k=0;i<=n;i++)
    {
        if (k) k--;
        if (rank[i]==1)
        {
            k=0;
            continue;
        }
        while (s[i+k]==s[sa[rank[i]-1]+k]) k++;
        height[rank[i]]=k;
    }
    for (i=1;i<=n;i++)
      f[i][0]=height[i];
    for (k=1;(1<<k)<=n;k++)
      for (i=1;i<=n;i++)
        f[i][k]=min(f[i][k-1],f[i+(1<<k-1)][k-1]);
    for (i=1;i<=l;i++)
    {
        if ((tem=2*qry(rank[i],rank[2*l-i+2])-1)>ans)
        {
            ans=tem;
            id=i;
        }
        if ((tem=2*qry(rank[i],rank[2*l-i+3]))>ans)
        {
            ans=tem;
            id=i;
        }
    }
    for (i=id-ans/2;i<=id+(ans+1)/2-1;i++)
      putchar(s[i]);
    putchar('\n');
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值