您的位置:首页 > 其它

URAL 1297 后缀数组+RMQ求串的最长回文子串

2016-11-06 19:23 423 查看
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

inputoutput
ThesampletextthatcouldbereadedthesameinbothordersArozaupalanalapuazorA

ArozaupalanalapuazorA

首先把原始字符串倒转过来,然后接在原始字符串的后面,中间用一个不可能出现的字符隔开。枚举每一个原始字符串中的字符以它为中心(分为奇数和偶数两种情况)进行查找,比如对于下标为i的字符,当回文串为奇数时,我们要求的就是i的后缀与2*n-i的
后缀的最长公共前缀了,偶数是2*n-i+1。利用RMQ求height数组,进而求LCP,函数如同多个子串的lcp函数

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define inf 0x3f3f3f3f
#include<algorithm>
using namespace std;
const int MAX=2020;
string str;
int n;
int wa[MAX],wb[MAX],wsf[MAX],wv[MAX],sa[MAX];
int rank[MAX],height[MAX],r[MAX];
int cmp(int *r,int a,int b,int k){
return (r[a]==r[b])&&(r[a+k]==r[b+k]);
}
void da(int *r,int *sa,int n,int m)//此处N比输入的N要多1,为人工添加的一个字符,用于避免cmp时越界
{
int i,j,p,*x=wa,*y=wb,*t;
for(i=0;i<m;i++) wsf[i]=0;
for(i=0;i<n;i++) wsf[x[i]=r[i]]++;
for(i=1;i<m;i++) wsf[i]+=wsf[i-1];
for(i=n-1;i>=0;i--) sa[--wsf[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++) wsf[i]=0;
for(i=0;i<n;i++) wsf[wv[i]]++;
for(i=1;i<m;i++) wsf[i]+=wsf[i-1];
for(i=n-1;i>=0;i--) sa[--wsf[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 *sa,int n)//得到height数组,

{
int i,j,k=0;
for(i=1;i<=n;i++)
rank[sa[i]]=i; //名次是从1~n,sa[]=(0~n-1)位置
for(i=0;i<n;i++)
{
if(k) k--;
else k=0;
j=sa[rank[i]-1];
while(r[i+k]==r[j+k])
k++;
height[rank[i]]=k;
}
}
int dp[MAX][20];
int RMQ_init()
{
for(int i=1;i<=n;i++)
dp[i][0]=height[i];
int len=log(n)/log(2);
for(int j=1;j<=len;j++)
for(int i=1;i<=n;i++)
if(i+(1<<j)-1<=n)
dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
}
int query(int l,int r)
{
int k=log(r-l+1)/log(2);
return min(dp[l][k],dp[r-(1<<k)+1][k]);
}
int lcp(int l,int r)
{
if(l==r)
return n-l;
if(rank[l]>rank[r])
return query(rank[r]+1,rank[l]);
else
return query(rank[l]+1,rank[r]);
}
void print(int x,int lenx,int flag)
{
if(flag==0)
for(int i=x-(lenx-1)/2;i<=x+(lenx-1)/2;i++)
cout<<str[i];
else
for(int i=x-(lenx/2);i<=x+(lenx/2)-1;i++)
cout<<str[i];
cout<<endl;
}
int main()
{
while(cin>>str)
{
int len=str.length();
for(int i=0;i<=len-1;i++)
r[i]=str[i];
r[len]=129;
for(int i=2*len;i>=len+1;i--)
r[i]=str[2*len-i];
n=2*len+1;
r
=0;
da(r,sa,n+1,150);
calheight(r,sa,n);
RMQ_init();
int x1,x2;
int lenx1=1,lenx2=0;
for(int i=1;i<=len-1;i++)
{
if(r[i]==129)
continue;
int j=2*len-i;
if(lenx1<(2*lcp(i,j)-1))
{
lenx1=2*lcp(i,j)-1;
x1=i;
}
j=2*len-i+1;
if(lenx2<2*lcp(i,j))
{
lenx2=2*lcp(i,j);
x2=i;
}
}
if(max(lenx1,lenx2)==1)
{
cout<<str[0]<<endl;
continue;;
}
if(lenx1==lenx2)
{
if(x1>x2)
print(x2,lenx2,1);
else
print(x1,lenx1,0);
}
else
{
if(lenx1>lenx2)
print(x1,lenx1,0);
else
print(x2,lenx2,1);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: