ura 1297. Palindrome 最长回文子串 后缀数组+RMQ

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

 

 

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
///后缀数组  倍增算法
const int maxn=50000;
char str[maxn];
int wa[maxn],wb[maxn],wv[maxn],wn[maxn],a[maxn],sa[maxn];
int cmp(int* r,int a,int b,int l)
{return r[a]==r[b]&&r[a+l]==r[b+l];}
/**n为字符串长度,m为字符的取值范围,r为字符串。后面的j为每次排
序时子串的长度*/
void DA(int* r,int* sa,int n,int m)
{
    int i,j,p,*x=wa,*y=wb,*t;
    ///对R中长度为1的子串进行基数排序
    for(i=0;i<m;i++)wn[i]=0;
    for(i=0;i<n;i++)wn[x[i]=r[i]]++;
    for(i=1;i<m;i++)wn[i]+=wn[i-1];
    for(i=n-1;i>=0;i--)sa[--wn[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++)wn[i]=0;
        for(i=0;i<n;i++)wn[wv[i]]++;
        for(i=1;i<m;i++)wn[i]+=wn[i-1];
        for(i=n-1;i>=0;i--)sa[--wn[wv[i]]]=y[i];
        ///当p=n的时候,说明所有串都已经排好序了
        ///在第一次排序以后,_rank数组中的最大值小于p,所以让m=p
        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++;
    }
    return;
}
///后缀数组  计算height数组
/**
height数组的值应该是从height[1]开始的,而且height[1]应该是等于0的。
原因是,+因为我们在字符串后面添加了一个0号字符,所以它必然是最小的
一个后缀。而字符串中的其他字符都应该是大于0的(前面有提到,使用倍
增算法前需要确保这点),所以排名第二的字符串和0号字符的公共前缀
(即height[1])应当为0.在调用calheight函数时,要注意height数组的范
围应该是[1..n]。所以调用时应该是calheight(r,sa,n)
而不是calheight(r,sa,n+1)。*/
int _rank[maxn],height[maxn];
void calheight(int* r,int* sa,int n)
{
    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++);
    return;
}
//RMQ 求任意区间的最小值
int d[maxn];
int dpmin[maxn][25];
void creat_dpmin(int n)
{
    int i,j;
    for(i=1;i<=n;i++) dpmin[i][0]=d[i];
    for(j=1;j<=log(double(n+1))/log(2.0);j++)
    {
        for(i=1;i+(1<<j)-1<=n;i++)
        {
            dpmin[i][j]=min(dpmin[i][j-1],dpmin[i+(1<<(j-1))][j-1]);
        }
    }
}
//求任意区间的最小值
int get_min(int x,int y)
{
    int k=(int)(log(double(y-x+1))/log(2.0));
    return min(dpmin[x][k],dpmin[y-(1<<k)+1][k]);
}
//后缀数组 + RMQ
//求第i个后缀和第j个后缀的最长公共前缀
int get_min_lcp(int x,int y)
{
    x=_rank[x],y=_rank[y];
    if(x>y) swap(x,y);
    x++;//利用height数组
    int k=(int)(log(double(y-x+1))/log(2.0));
    return min(dpmin[x][k],dpmin[y-(1<<k)+1][k]);
}
int main()
{
        //scanf("%s",str);//待处理字符串
        //后缀数组 倍增算法 使用方法
        /**
        在使用倍增算法前,需要保证r数组的值均大于0。然后要在原字
        符串后添加一个0号字符,具体原因参见罗穗骞的论文。这时候,
        若原串的长度为n,则实际要进行后缀数组构建的r数组的长度应
        该为n+1.所以调用DA函数时,对应的n应为n+1.*/
        /*int n=strlen(str);
        for(int i=0;i<n;i++) a[i]=(int)str[i];
        a[n]=0;
        DA(a,sa,n+1,256);
        calheight(a,sa,n);*/
        //....................................
    int n1,n2;
    char str1[maxn],str2[maxn];
    scanf("%s",str1);memcpy(str2,str1,sizeof(str1));
    n1=strlen(str1),n2=strlen(str2);
    reverse(str2,str2+n2);
    for(int i=0;i<n1;i++) a[i]=(int)str1[i];
    a[n1]=1;
    for(int i=0;i<n2;i++) a[n1+i+1]=(int) str2[i];
    int n=n1+n2+1;
    a[n]=0;
    DA(a,sa,n+1,256);
    calheight(a,sa,n);
    //...............
    for(int i=1;i<=n;i++) d[i]=height[i];
    creat_dpmin(n);
    int ans=0,pos;
    for(int i=0;i<n1;i++)//枚举回文串中心
    {
        int t=get_min_lcp(i,n1+n1-i)*2-1;//回文串是奇数情况 t回文串长度
        if(t>ans) ans=t,pos=i;
        if(i>0)//回文串是偶数数情况 t回文串长度
        {
            t=get_min_lcp(i,n1+n1-i+1)*2;
            if(t>ans) ans=t,pos=i;
        }
    }
    //奇数 偶数
    if(ans&1) for(int i=pos-ans/2;i<=pos+ans/2;i++) printf("%c",str1[i]);
    else for(int i=pos-ans/2;i<=pos+ans/2-1;i++) printf("%c",str1[i]);
    printf("/n");
    return 0;
}
//abcafglcba

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值