首页 > 代码库 > 后缀数组 - 求最长回文子串 + 模板题 --- ural 1297

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

 

1297. Palindrome

Time Limit: 1.0 second
Memory 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» 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 
 

 

Mean: 

 给你一个字符串,让你求这个字符串的最长回文子串,并输出。

analyse:

 当然,求最长回文串有很多方法,比如:Manacher算法,时间复杂度O(n)。但最近学习了后缀数组,这里就主要介绍一下用后缀数组的方法。

用后缀数组怎么求回文串呢?原理和上一篇求最长公共子序列一样,我们把s1反转后接到s1后面得到S串,那么s1的最长回文串必定存在于S中,我们只需要求一下S的height数组,然后寻找来自于不同的两个串的height[i]的最大值,然后记录一下开始位置和长度,最后输出即可。

Time complexity:O(nlogn)

 

Source code:

 

// Memory   Time// 1347K     0MS// by : Snarl_jsb// 2014-09-24-20.34#include<algorithm>#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<vector>#include<queue>#include<stack>#include<map>#include<string>#include<climits>#include<cmath>#define N 1234<<1#define LL long longusing namespace std;int num[N];char str[N];namespace Suf{    int sa[N], rank[N], height[N];    int wa[N], wb[N], wv[N], wd[N];    int cmp(int *r, int a, int b, int l)    {        return r[a] == r[b] && r[a+l] == r[b+l];    }    void da(int *r, int n, int m)     //    参数:*r---str转化为int型后的数组  n----数组长度  m---元素的范围    {        int i, j, p, *x = wa, *y = wb, *t;        for(i = 0; i < m; i ++) wd[i] = 0;        for(i = 0; i < n; i ++) wd[x[i]=r[i]] ++;        for(i = 1; i < m; i ++) wd[i] += wd[i-1];        for(i = n-1; i >= 0; i --) sa[-- wd[x[i]]] = i;        for(j = 1, p = 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 ++) wd[i] = 0;            for(i = 0; i < n; i ++) wd[wv[i]] ++;            for(i = 1; i < m; i ++) wd[i] += wd[i-1];            for(i = n-1; i >= 0; i --) sa[-- wd[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 ++;        }    }    void calcHeight(int *r, int n)         //  求height数组    {        int i, j, k = 0;        for(i = 1; i <= n; i ++) rank[sa[i]] = i;        for(i = 0; i < n; height[rank[i ++]] = k)        {            for(k ? k -- : 0, j = sa[rank[i]-1]; r[i+k] == r[j+k]; k ++);        }    }}using namespace Suf;int main(){    ios_base::sync_with_stdio(false);    cin.tie(0);//    freopen("C:\\Users\\ASUS\\Desktop\\cin.cpp","r",stdin);//    freopen("C:\\Users\\ASUS\\Desktop\\cout.cpp","w",stdout);    while(~scanf("%s",str))    {        int len=strlen(str);        for(int i=0;i<len;++i)            num[i]=str[i];        int len1=len;        num[len]=1;        for(int i=len-1;i>=0;--i)            num[++len]=str[i];        num[++len]=0;        da(num,len+1,250);        calcHeight(num,len);        int maxx=1;        int start=0;        int l,r;        for(int i=1;i<=len;++i)        {            l=min(sa[i],sa[i-1]);            r=max(sa[i],sa[i-1]);            if((l<len1&&r>len1)&&(l+height[i])==(len-r))            {                if(height[i]>maxx)                {                    maxx=height[i];                    start=l;                }                else if(height[i]==maxx)                {                    start=min(start,l);                }            }        }        for(int i=start,j=0;j<maxx;++i,++j)            printf("%c",num[i]);        puts("");    }    return 0;}

  

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