Palindrome+ural+后缀数组之求最长回文串

Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64

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

input
ThesampletextthatcouldbereadedthesameinbothordersArozaupalanalapuazorA
output
ArozaupalanalapuazorA


解决方案:可将字符串逆过来,接在原串的后面,中间用一个特殊字符隔开,这就变成了求相关后缀的最长公共前缀了。但也要经过一定的预处理,由于可能是奇数长度的回文,也可能是偶数长度的回文,我们可以再两个字符之间插入一个特殊字符,整串的首尾也插入一个特殊字符,这样就把每个回文都转成的奇数长度的回文了。
code:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int nMax = 2000001;
int sa[nMax], rank[nMax], height[nMax];
int wa[nMax], wb[nMax], wv[nMax], wd[nMax];
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为待匹配数组  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 calHeight(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 ++);
    }
}
int dp[nMax][30];
void initRMQ(int len)
{
    for(int i=1; i<=len; i++) dp[i][0]=height[i];
    for(int j=1; (1<<j)<=len; j++)
        for(int i=0; i+(1<<j)-1<=len; i++)
        {
            dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
        }
}
int lcp(int a,int b)
{
    a=rank[a];
    b=rank[b];
    if(a>b) swap(a,b);
    a++;
    int k=0;
    while((1<<(k+1))<=(b-a+1)) k++;
    return min(dp[a][k],dp[b-(1<<k)+1][k]);
}
int num[nMax];
char text[nMax];
int main()
{
    while(~scanf("%s",text))
    {
        int len=strlen(text);
        int maxn=0;
        int j=0;
        num[j++]=100;
        for(int i=0; i<len; i++)
        {
            num[j++]=int(text[i]-31);
            maxn=max(maxn,int(text[i]-31));
            num[j++]=100;
        }
        num[j++]=++maxn;
        num[j++]=100;
        for(int i=len-1; i>=0; i--)
        {
            num[j++]=int(text[i]-31);
            num[j++]=100;
        }
        num[j]=0;
      /*  for(int i=0;i<=j;i++){
            printf("%d ",num[i]);
        }
        cout<<endl;*/
       // cout<<j<<endl;
        da(num,j+1,103);
        calHeight(num,j);
        initRMQ(j);
        int mmax=0,L=0,st=0;
        for(int i=1; i<2*len+1; i++)
        {
              L=lcp(i+1,j-i);
             // cout<<L<<endl;
              if(L>mmax){
                mmax=L;
                st=i;
              }
        }
        int en=st+mmax;
        st-=mmax;
       // cout<<st<<" "<<en<<endl;
        for(int i=st; i<=en; i++)
        {   if(i%2!=0)
            printf("%c",text[i/2]);
        }
        printf("\n");
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值