您的位置:首页 > 其它

ural1297——Palindrome(后缀数组)

2013-04-14 11:15 274 查看
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

ThesampletextthatcouldbereadedthesameinbothordersArozaupalanalapuazorA

output
ArozaupalanalapuazorA

解析:

         后缀数组求最长回文子串。。。

         将原串反接在原串后面。。。这样,问题就变成了求两个后缀之间的最长公共前缀。。。

         图示:



(纯手工珍藏版题解)

        ps:由于此代码属于本人yy版,可能有些冗长,故不建议作为模板

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 100005
#define maxm 250
int len;         char ss[1100];
int wa[maxn],wb[maxn],wv[maxn],ws[maxn];

int cmp(int *r,int a,int b,int l)
{
return (r[a]==r[b] && r[a+l]==r[b+l]);
}

void DA(int n,int *sa,int m,int *s)
{
int i,j,p,*x=wa,*y=wb,*t;
for(int i=0;i<m;i++)ws[i]=0;                       //基数排序
for(int i=0;i<n;i++)ws[x[i]=s[i]]++;               //
for(int i=1;i<m;i++)ws[i]+=ws[i-1];                //     (长度为一)
for(int i=n-1;i>=0;i--)sa[--ws[x[i]]]=i;           //
for(int 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++)ws[i]=0;                       //基数排序
for(i=0;i<n;i++)ws[wv[i]]++;                   //
for(i=1;i<m;i++)ws[i]+=ws[i-1];                //
for(i=n-1;i>=0;i--)sa[--ws[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++;
}
return ;
}
int rank[maxn],height[maxn];
void calheight(int *s,int *sa,int n)
{
int i,j,k=0;
for(i=0;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];s[i+k]==s[j+k];k++);
return ;
}

void read()
{
freopen("ural1297.in","r",stdin);
freopen("ural1297.out","w",stdout);
}
int s[maxn],sa[maxn];
void work()
{
int l=0;
scanf("%s",ss);
len=strlen(ss);
for(int i=0;i<len;i++)
s[l++]=ss[i];
s[l++]='$';
for(int i=len-1;i>=0;i--)
s[l++]=ss[i];
s[l]='#';
DA(l+1,sa,maxm,s);
calheight(s,sa,l);
int start,start1,length,len_len;
int le=0;                    int st;
for(int i=0;i<len;i++)
{
int best=1000000;
if(rank[i]<rank[l-i-1])            //处理奇数回文串
for(int j=rank[i]+1;j<=rank[l-i-1];j++)best=min(best,height[j]);
else for(int j=rank[l-i-1]+1;j<=rank[i];j++)best=min(best,height[j]);
start=i-best+1;
length=2*best-1;

best=1000000;
if(rank[i]<rank[l-i])            //处理偶数回文串
for(int j=rank[i]+1;j<=rank[l-i];j++)best=min(best,height[j]);
else for(int j=rank[l-i]+1;j<=rank[i];j++)best=min(best,height[j]);
start1=i-best;
len_len=2*best;

if(length>le){le=length;st=start;}
if(len_len>le){le=len_len;st=start1;}
}
for(int i=st;i<=st+le-1;i++)printf("%c",s[i]);
printf("\n");
}

int main()
{
read();
work();
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: