URAL 1297. Palindrome(后缀数组 求最长回文子串)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1297

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 output
ThesampletextthatcouldbereadedthesameinbothordersArozaupalanalapuazorA
ArozaupalanalapuazorA

代码如下:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
#define N 2047
int wa[N], wb[N], wv[N], ws1[N];
int rank1[N]; //名次数组
int  height[N]; //排名相邻的两个后缀的最长公共前缀
int sa[N]; //sa为后缀数组,n个后缀从小到大进行排序之后把排好序的后缀的开头位置
char s[N];
int a[N], n;
int dp[N][47];

int minn(int a,int b)
{
    return a>b?b:a;
}

int cmp(int *r,int a,int b,int l)
{
    return r[a]==r[b] && r[a+l]==r[b+l];
}

void get_sa(int *r, int *sa, int n, int m) //倍增算法
{
    int i,j,p,*x=wa,*y=wb,*t;
    for(i=0; i<m; i++) ws1[i]=0;
    for(i=0; i<n; i++) ws1[x[i]=r[i]]++;
    for(i=1; i<m; i++) ws1[i]+=ws1[i-1];
    for(i=n-1; i>=0; i--) sa[--ws1[x[i]]]=i; //对长度为1的字符串排序
    for(p=1,j=1; p<n; j*=2,m=p)
    {
        for(p=0,i=n-j; i<n; i++) y[p++]=i;
        for(i=0; i<n; i++) if(sa[i]>=j) y[p++]=sa[i]-j; //第二关键字排序结果

        for(i=0; i<n; i++) wv[i]=x[y[i]];
        for(i=0; i<m; i++) ws1[i]=0;
        for(i=0; i<n; i++) ws1[wv[i]]++;
        for(i=1; i<m; i++) ws1[i]+=ws1[i-1];
        for(i=n-1; i>=0; i--) sa[--ws1[wv[i]]]=y[i]; //第一关键字排序

        for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1; i<n; i++)
            x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++; //更新rank数组
    }
    return;
}
void get_height(int *r, int *sa, int n) //求height数组
{
    int i, j, k=0;
    for(i=1; i<=n; i++) rank1[sa[i]]=i;
    for(i=0; i<n; height[rank1[i++]]=k)
        for(k?k--:0,j=sa[rank1[i]-1]; r[i+k]==r[j+k]; k++);
    return;
}
void RMQ()
{
    memset(dp,127,sizeof(dp));
    for(int i = 1; i <= n*2+1; i++)
        dp[i][0] = height[i];
    for(int j = 1; (1<<j) <= 2*n+1; j++)
        for(int i = 1; i+(1<<j)-1 <= 2*n+1; i++)
            dp[i][j] = minn(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}

int lcp(int l,int r)//求最长公共前缀
{
    int a = rank1[l], b = rank1[r];
    if(a > b)
        swap(a,b);
    a++;
    int t=(int)(log(double(b-a+1))/log(2.00));
    return minn(dp[a][t],dp[b-(1<<t)+1][t]);
}

int main()
{
    int res;
    while(~scanf("%s",s))
    {
        int maxx = 0;
        n = strlen(s);
        for(int i = 0; i < n; i++)
            a[i] = (int)s[i];
        a[n] = 1;
        for(int i = 0; i < n; i++)
            a[i+n+1] = (int)s[n-i-1];
        int LEN = 2*n+1;
        a[LEN] = 0;
        get_sa(a,sa,LEN+1,320);
        get_height(a,sa,LEN);
        RMQ();
        int anslen = 0, idx = 0;
        for(int i = 0; i < n; i++)
        {
            int t = lcp(i,LEN-i-1);
            if(2*t-1 > anslen)//多算了一次中心字母
            {
                anslen = 2*t-1;
                idx = i;
            }
            if(s[i] == s[i+1])
            {
                t = lcp(i+1,LEN-i-1);
                if(2*t > anslen)
                {
                    anslen = 2*t;
                    idx = i;
                }
            }
        }
        idx = idx-(anslen-1)/2;
        for(int i = 0; i < anslen; i++)
            printf("%c",s[idx+i]);
        printf("\n");
    }
    return 0;
}
时间: 2024-10-11 22:22:12

URAL 1297. Palindrome(后缀数组 求最长回文子串)的相关文章

URAL - 1297 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 alrea

后缀数组 - 求最长回文子串 + 模板题 --- ural 1297

1297. Palindrome Time Limit: 1.0 secondMemory Limit: 16 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» s

URAL 1297. Palindrome(后缀数组求最大回文串)

题目大意:给你一串字符串,让你求出来它存在的最长连续的回文串. 解题思路:先把字符串逆序加到数组中,然后用后缀数组求解.两种方法:1,枚举排名,直接比较rank相同的字符串的位置差是不是len.如果是的话,就记录求解:2,枚举地址,求第i地址与第2*len-i+1的lcp的最大值. PS:需要注意如果多解输出靠前的字符串. 两种写法写在了一起,分别是Del,和Del1函数. 1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB T

URAL 1297 后缀数组:求最长回文子串

思路:这题下午搞了然后一直WA,后面就看了Discuss,里面有个数组:ABCDEFDCBA,这个我输出ABCD,所以错了. 然后才知道自己写的后缀数组对这个回文子串有bug,然后就不知道怎么改了. 然后看题解,里面都是用RMQ先预处理任意两个后缀的最长公共前缀,因为不太知道这个,所以又看了一下午,嘛嘛-- 然后理解RMQ和后缀一起用的时候才发现其实这里不用RMQ也可以,只要特殊处理一下上面这个没过的例子就行了,哈哈--机智-- 不过那个国家集训队论文里面正解是用RMQ做的,自己还得会和RMQ一

JavaScript算法----给定一个长度为N的串,求最长回文子串。

/* *给定一个长度为N的串,求最长回文子串. */ function returnStr(str){ console.log(str); var arr = [],s = ""; for(var i=0;i<str.length;i++){ s = ""; if(str.charAt(i)==str.charAt(i+1)){ var j=0; while(str.charAt(i+j+1)==str.charAt(i-j)){ s = str.charAt

O(n) 求最长回文子串的 Manacher 算法

Manacher是一个可以在O(n)的时间内求出一个长度为n的字符串的算法. 以为回文子串有偶数长度,也有奇数长度,分别处理会很不方便. 所以在每两个字符中间插入一个无关字符,如‘#’,这样所有的回文子串都变为奇数长度. 两端在添加不同的无关字符防止匹配时越界. 如: abba 变成 $#a#b#b#a#& 预处理代码: void Prepare() { l = strlen(Str); S[0] = '$'; for (int i = 0; i <= l - 1; i++) { S[(i

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".

求最长回文子串,O(n)复杂度

最长回文子串问题-Manacher算法 最长回文串问题是一个经典的算法题. 0. 问题定义 最长回文子串问题:给定一个字符串,求它的最长回文子串长度. 如果一个字符串正着读和反着读是一样的,那它就是回文串.下面是一些回文串的实例: 12321 a aba abba aaaa tattarrattat(牛津英语词典中最长的回文单词) 1. Brute-force解法 对于最长回文子串问题,最简单粗暴的办法是:找到字符串的所有子串,遍历每一个子串以验证它们是否为回文串.一个子串由子串的起点和终点确定

manacherO(n)求最长回文子串 hihocoder1032

原文地址:https://segmentfault.com/a/1190000003914228   http://blog.csdn.net/synapse7/article/details/18908413 灰常不错的学习资料 先预处理下:在每个字符的两边都插入一个特殊的符号,比如abba变成#a#b#b#a#,aba变成 #a#b#a#(因为Manacher算法只能处理奇数长度的字符串).同时,为了避免数组越界,在字符串开头添加另一特殊符号,比如$#a#b#a#. 以字符串32123432