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


/*穷举每一位,然后计算以这个字符为中心的最长回文子串。注意这里要分两
种情况,一是回文子串的长度为奇数,二是长度为偶数。两种情况都可以转化为
求一个后缀和一个反过来写的后缀的最长公共前缀。具体的做法是:将整个字符
串反过来写在原字符串后面,中间用一个特殊的字符隔开。这样就把问题变为了
求这个新的字符串的某两个后缀的最长公共前缀。
这个做法的时间复杂度为 O(nlogn)。如果 RMQ 问题用时间为 O(n)的方法预
处理,那么本题的时间复杂度可以降为 O(n)。*/
//刚开始以为只要求出height数组最大值就行啦,abcdefdcba,输出abcd,
//明显是错误的,还是得分奇偶来算
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
#define N 2005
int t1[N],t2[N],x[N],c[N],sa[N],s[N],ran[N],height[N],d[N][30];
char str[N];
void build_sa(int *s,int n,int m)
{
	int *x=t1,*y=t2,i,k;
	for(i=0;i<m;i++) c[i]=0;
	for(i=0;i<n;i++) c[x[i]=s[i]]++;
	for(i=1;i<m;i++) c[i]+=c[i-1];
	for(i=n-1;i>=0;i--) sa[--c[x[i]]]=i;
	for(k=1;k<=n;k<<=1)
	{
		int p=0;
		for(i=n-k;i<n;i++) y[p++]=i;
		for(i=0;i<n;i++) if(sa[i]>=k) y[p++]=sa[i]-k;
		for(i=0;i<m;i++) c[i]=0;
		for(i=0;i<n;i++) c[x[y[i]]]++;
		for(i=1;i<m;i++) c[i]+=c[i-1];
		for(i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];
		swap(x,y);
		p=1; x[sa[0]]=0;
        for(i=1;i<n;i++)
			x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;
		if(p>=n)
			break;
		m=p;
	}
}
void getheight(int n)
{
	int i,k=0,j;
	for(i=0;i<=n;i++)
		ran[sa[i]]=i;
	for(i=0;i<n;i++)
	{
		if(k) k--;
		j=sa[ran[i]-1];
		while(s[j+k]==s[i+k])  k++;
		height[ran[i]]=k;
	}
}
int min(int a,int b)
{
    return a<b?a:b;
}
void rqminit(int n)
{
    int i,j;
    memset(d,0,sizeof(d));
    for(i=1;i<=n;i++)
        d[i][0]=height[i];
    for(j=1;(1<<j)<=n;j++)
        for(i=1;i+(1<<j)-1<=n;i++)
           d[i][j]=min(d[i][j-1],d[i+(1<<(j-1))][j-1]);
}
int rmq(int l,int r)
{
    int k=0;
    while((1<<(k+1))<=r-l+1) k++;
    return min(d[l][k],d[r-(1<<k)+1][k]);
}
int lcp(int a,int b)
{
    a=ran[a]; b=ran[b];
    if(a>b) swap(a,b);
    return rmq(a+1,b);
}
int main()
{
    int m,n,u,ans,i,temp;
    while(scanf("%s",str)!=EOF)
    {
        n=strlen(str);m=n;
        for(i=0;str[i]!='\0';i++)
            s[i]=str[i];
        s[n]='$';
        for(i=n-1;i>=0;i--)
            s[++n]=str[i];
        build_sa(s,n+1,200);
        getheight(n);
        rqminit(n);
        ans=-1;
        for(i=0;i<m;i++)
        {
            temp=2*lcp(i,n-i)-1;
            if(temp>ans)
            {//奇回文字串:以i为中心,最大回文串长度等于2*lcp(suffix(i),suffix(n-i)-1
                ans=temp;
                u=i-temp/2;
            }
            temp=2*lcp(i+1,n-i);
            if(temp>ans)
            {//偶回文字串:以i,i+1为中心,最大回文串长度等于2*lcp(suffix(i+1),suffix(n-i))
                ans=temp;
                u=i+1-temp/2;
            }
        }
        //printf("%d %d\n",u,ans);
        for(i=u;i<ans+u;i++)
            printf("%c",str[i]);
        printf("\n");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值