ural 1297 后缀数组求最长回文串

C - C
Time Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u
Appoint description:

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


题意:给定一个字符串,求最长回文串。

解题思路:将字符串反转添加到后面,中间用一个字符隔开,然后枚举,求两个后缀的最长公共前缀。

代码:

/* ***********************************************
Author :xianxingwuguan
Created Time :2014-1-28 10:15:15
File Name :\acm\代码\4.cpp
************************************************ */

#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int maxn=10000;
int rank[maxn],sa[maxn],c[maxn],t1[maxn],t2[maxn],height[maxn];
char str[maxn];
int ss[maxn];
void da(int *str,int n,int m)
{
      int i,j,k,p,*x=t1,*y=t2;
      for(i=0;i<m;i++)c[i]=0;
      for(i=0;i<n;i++)c[x[i]=str[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)
      {
	     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]]==y[sa[i-1]]&&y[sa[i]+k]==y[sa[i-1]+k]?p-1:p++;
	     if(p>=n)break;
	     m=p;
      }
}
void calheight(int *str,int n)
{
      int i,j,k=0;
      for(i=0;i<=n;i++)rank[sa[i]]=i;
      for(i=0;i<n;i++)
      {
	     if(k)k--;
	     j=sa[rank[i]-1];
	     while(str[i+k]==str[j+k])k++;
	     height[rank[i]]=k;
      }
}
int Log[maxn],best[20][maxn];
void init(int n)
{
      int i,j;
      Log[0]=-1;
      for( i=1;i<=n;i++)
	    Log[i]=(i&(i-1))?Log[i-1]:Log[i-1]+1;
      //cout<<"Log:";for(i=0;i<=n;i++)cout<<Log[i]<<" ";cout<<endl;
      for(i=1;i<=n;i++)best[0][i]=height[i];
      for(i=1;i<=Log[n];i++)
	     for(j=1;j+(1<<i)-1<=n;j++)
		    best[i][j]=min(best[i-1][j],best[i-1][j+(1<<(i-1))]);
}
int lcp(int a,int b)
{
      a=rank[a];
      b=rank[b];
      if(a>b)swap(a,b);
      a++;
      int t=Log[b-a+1];
      return min(best[t][a],best[t][b-(1<<t)+1]);
}
int main()
{
      freopen("data.in","r",stdin);
      freopen("data.out","w",stdout);
      int i,j,k,m,n;
      while(~scanf("%s",str))
      {
	     n=strlen(str);
	    // cout<<"n="<<n<<endl;
	     int len=0;
	     for(i=0;i<n;i++)ss[len++]=str[i];
	     ss[len++]=1;
	     for(i=n-1;i>=0;i--)ss[len++]=str[i];
	     ss[len]=0;
	     da(ss,len+1,300);
	     calheight(ss,len);
         //cout<<"sa:";for(i=0;i<=len;i++)cout<<sa[i]<<" ";cout<<endl;      
         //cout<<"rank:";for(i=0;i<=len;i++)cout<<rank[i]<<" ";cout<<endl;      
         //cout<<"height:";for(i=0;i<=len;i++)cout<<height[i]<<" ";cout<<endl;      
	     
	     init(len);
	   //  cout<<"hahha"<<endl;
	     int ans=0,w;
	     for(i=0;i<n;i++)
	     {
		    j=lcp(i,len-i);
		    if(ans<2*j)ans=2*j,w=i-j;
		    j=lcp(i,len-i-1);
		    if(ans<2*j-1)ans=2*j-1,w=i-j+1;
  	     }
	     str[ans+w]=0;
	     printf("%s\n",str+w);
	    // cout<<ans<<" "<<w<<endl;
	   // while(cin>>i>>j)cout<<lcp(i,j)<<endl;
      }
      return 0;
}




 



ural 1297 后缀数组求最长回文串

上一篇:英语口语集训心得


下一篇:hdu 1503 Advanced Fruits (公共子序列 的输出)