ural1297之最长回文子串

http://acm.timus.ru/problem.aspx?space=1&num=1297

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

题目要求求出给出的字符串的最长回文子串

分析:穷举每一位i,以i为中心的最长回文串,则只需要求出i+1和i-1倒过来的后缀最长公共前缀,或者求出i和i-1(偶数长度回文串)倒过来的最长公共前缀

所以只需要将原串反转放到原串后面然后求出height再用LCP枚举求出长度即可

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <iomanip>
#define INF 99999999
typedef long long LL;
using namespace std;

const int MAX=2*1000+10;
int *rank,r[MAX],sa[MAX],height[MAX];
int wa[MAX],wb[MAX],wm[MAX],L[MAX][32];
char s[MAX];

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

void makesa(int *r,int *sa,int n,int m){
	int *x=wa,*y=wb,*t;
	for(int i=0;i<m;++i)wm[i]=0;
	for(int i=0;i<n;++i)wm[x[i]=r[i]]++;
	for(int i=1;i<m;++i)wm[i]+=wm[i-1];
	for(int i=n-1;i>=0;--i)sa[--wm[x[i]]]=i;
	for(int i=0,j=1,p=0;p<n;j=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<m;++i)wm[i]=0;
		for(i=0;i<n;++i)wm[x[y[i]]]++;
		for(i=1;i<m;++i)wm[i]+=wm[i-1];
		for(i=n-1;i>=0;--i)sa[--wm[x[y[i]]]]=y[i];
		for(t=x,x=y,y=t,i=p=1,x[sa[0]]=0;i<n;++i){
			x[sa[i]]=cmp(y,sa[i],sa[i-1],j)?p-1:p++;
		}
	}
	rank=x;
}

void calheight(int *r,int *sa,int n){
	for(int i=0,j=0,k=0;i<n;height[rank[i++]]=k){
		for(k?--k:0,j=sa[rank[i]-1];r[i+k] == r[j+k];++k);
	}
}

void InitRMQ(int n){
	int l=(int)(log(n*1.0)/log(2.0));//2*l<=n
	for(int i=1;i<=n;++i)L[i][0]=height[i];
	for(int j=1;j<=l;++j){
		for(int i=1;i<=n+1-(1<<j);++i){//i+2^j-1<=n
			L[i][j]=min(L[i][j-1],L[i+(1<<(j-1))][j-1]);
		}
	}
}

int LCP(int a,int b){
	int i=rank[a],j=rank[b];
	if(i>j)swap(i,j);
	++i;
	int l=(int)(log(j-i+1.0)/log(2.0));//2^j<=j-i+1
	return min(L[i][l],L[j-(1<<l)+1][l]);
}

int main(){
	while(~scanf("%s",s)){
		int n=strlen(s),len=n;
		s[n]='#';
		for(int i=n-1;i>=0;--i)s[++n]=s[i];
		s[++n]='\0';
		for(n=0;s[n] != '\0';++n)r[n]=s[n];
		r[n]=0;
		makesa(r,sa,n+1,256);
		calheight(r,sa,n);
		InitRMQ(n);
		int sum=1,Id=0;
		for(int i=1;i<len;++i){
			if(2*LCP(i+1,n-i)+1>sum)sum=2*LCP(i+1,n-i)+1,Id=i-LCP(i+1,n-i);
			if(2*LCP(i,n-i)>sum)sum=2*LCP(i,n-i),Id=i-LCP(i,n-i);
		}
		for(int i=Id;i<Id+sum;++i)cout<<s[i];
		cout<<endl;
	}
	return 0;
}

你可能感兴趣的:(ural1297之最长回文子串)