Ural 1297. Palindrome 求最大回文串

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

input
ThesampletextthatcouldbereadedthesameinbothordersArozaupalanalapuazorA
output
ArozaupalanalapuazorA
Problem Author: Eugene Krokhalev
Problem Source: IX Open Collegiate Programming Contest of the High School Pupils (13.03.2004)

Manacher算法
这个算法有一个很巧妙的地方,它把奇数的回文串和偶数的回文串统一起来考虑了。这一点一直是在做回文串问题中时比较烦的地方。这个算法还有一个很好的地方就是充分利用了字符匹配的特殊性,避免了大量不必要的重复匹配。
算法大致过程是这样。先在每两个相邻字符中间插入一个分隔符,当然这个分隔符要在原串中没有出现过。一般可以用‘#’分隔。这样就非常巧妙的将奇数长度回文串与偶数长度回文串统一起来考虑了(见下面的一个例子,回文串长度全为奇数了),然后用一个辅助数组P记录以每个字符为中心的最长回文串的信息。P[id]记录的是以字符str[id]为中心的最长回文串,当以str[id]为第一个字符,这个最长回文串向右延伸了P[id]个字符。
原串: w aa bwsw f d
新串: # w # a # a # b # w # s # w # f # d #
辅助数组P: 1 2 1 2 3 2 1 2 1 2 1 4 1 2 1 2 1 2 1
这里有一个很好的性质,P[id]-1就是该回文子串在原串中的长度(包括‘#’)。如果这里不是特别清楚,可以自己拿出纸来画一画,自己体会体会。当然这里可能每个人写法不尽相同,不过我想大致思路应该是一样的吧。
好,我们继续。现在的关键问题就在于怎么在O(n)时间复杂度内求出P数组了。只要把这个P数组求出来,最长回文子串就可以直接扫一遍得出来了。
由于这个算法是线性从前往后扫的。那么当我们准备求P[i]的时候,i以前的P[j]我们是已经得到了的。我们用mx记在i之前的回文串中,延伸至最右端的位置。同时用id这个变量记下取得这个最优mx时的id值。
for(i=1; i<n; i++)
{
    if(MaxId>i)
    {
        p[i]=Min(p[2*id-i],MaxId-i);
    }
    else
    {
        p[i]=1;
    }
    while(a[i+p[i]]==a[i-p[i]])
    {
        p[i]++;
    }
    if(p[i]+i>MaxId)
    {
        MaxId=p[i]+i;
        id=i;
    }
}


求最大回文串
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 100000

char s[maxn];
int slen[maxn];
int len ;


void Manacher()
{
    int maxid=1;
    int id=1;
    len=strlen(s);
    for(int i=1; i<len; i++)
    {
        if(maxid>i)
            slen[i]=min(slen[2*id-i],maxid-i);
        else
            slen[i]=1;

        while(s[i+slen[i]]==s[i-slen[i]])
            slen[i]++;
        if(i+slen[i]>maxid)
        {
            maxid=i+slen[i];
            id=i;
        }
    }
}


int main()
{
    char s1[maxn];
    scanf("%s",s1+1);
    int i;
    for(i=1; s1[i]!='\0'; i++)
    {
        s[(i<<1)-1]='#';
        s[i<<1]=s1[i];
    }
    s[0]='?';
    s[(i<<1)-1]='#';
    s[i<<1]=s1[i];
    Manacher();
    int imax=0;
    for(int i=1; i<len; i++)
        if(slen[i]>slen[imax])
            imax=i;
    int beg=imax-slen[imax]+1,en=imax+slen[imax]-1;
    for(int i=beg; i<=en; i++)
    {
        if(s[i]!='#'&&s[i]!='?')
            printf("%c",s[i]);
    }
    printf("\n");
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cstring>
int n,inc,ans=0,pos=0;
char s[1010];
int main()
{
    scanf("%s",&s[1]);
    n=strlen(&s[1]);
    for (int i=1; i<=n; ++i)
    {
        inc=0;
        for (int j=0; (i-j)&&(i+j<=n); ++j)
            if (s[i-j] == s[i+j])  ++inc;
            else  break;
        if ((inc<<1)-1 > ans)
        {
            ans=(inc<<1)-1;
            pos=i-inc+1;
        }
        inc=0;
        for (int j=0; (i-j) && (i+j+1<=n); ++j)
            if (s[i-j] == s[i+j+1])  ++inc;
            else  break;
        if ((inc<<1) > ans)
        {
            ans=(inc<<1);
            pos=i-inc+1;
        }
    }
    s[pos+ans]='\0';
    printf("%s",&s[pos]);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值