1297. PalindromeTime Limit: 1.0 second
Memory Limit: 16 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.
InputThe 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.
OutputThe longest substring with mentioned property. If there are several such strings you should output the first of them.
Sample
|
思路:将原串的反向插到原串后面,中间用1隔开,最后在用0隔开。
于是变成了求新串的某两个后缀的最长公共前缀(为什么?自己思考)
不过求的过程要判断答案是否在原串的同一个位置(讲的感觉很不清。。。自己理解下哈)
这题我WA了20次左右才AC的 - -| 细节方面掌握的不好,主要还是对后缀数组的掌握欠缺
本题网址:http://acm.timus.ru/problem.aspx?space=1&num=1297
************************************************************************/
#include <iostream>
#include <string.h>
using namespace std;
#define max(a,b) (a>b?a:b)
#define min(a,b) (a>b?b:a)
const int maxn = 100000;
int wa[maxn],wb[maxn],wv[maxn],wws[maxn],a[maxn],sa[maxn],rank1[maxn],height[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 *r,int *sa,int n,int m)
{
int i,j,p,*x=wa,*y=wb,*t;
for(i=0;i<m;i++) wws[i]=0;
for(i=0;i<n;i++) wws[x[i]=r[i]]++;
for(i=1;i<m;i++) wws[i]+=wws[i-1];
for(i=n-1;i>=0;i--) sa[--wws[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++) wws[i]=0;
for(i=0;i<n;i++) wws[wv[i]]++;
for(i=1;i<m;i++) wws[i]+=wws[i-1];
for(i=n-1;i>=0;i--) sa[--wws[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;
}
void calheight(int *r,int *sa,int n)
{
int i,j,k=0;
for(i=1;i<=n;i++)rank1[sa[i]]=i;
for(i=0;i<n;height[rank1[i++]]=k)
for(k?k--:0,j=sa[rank1[i]-1];r[i+k]==r[j+k];k++);
}
int n,len;
bool ask(int a,int b)
{
if(a<len && b>len) return true;
else return false;
}
int main()
{
char str[1005],str1[1005];
int i,j,MAX,start;
while(cin>>str)
{
n = 0;
len = strlen(str);
for(i=0;i<len;i++)
str1[i] = str[len-1-i];
for(i=0;i<len;i++)
{
a[n++] = static_cast<int>(str[i]);
}
a[n++] = 1;
for(i=0;i<len;i++)
{
a[n++] = static_cast<int>(str1[i]);
}
a[n] = 0;
da(a,sa,n+1,250);
calheight(a,sa,n);
MAX = 1; //记得从1开始(我最早0开始,WA了很多次@ @)
start = 0;
int low,high;
for(i=1;i<=n;i++)
{
low = min(sa[i-1],sa[i]);
high = max(sa[i-1],sa[i]);
if(ask(low,high) && ((low+height[i]) == (n-high)))
{
if(MAX<height[i])
{
MAX = height[i];
start = low;
}
else if(MAX == height[i])
{
start = min(start,low);
}
}
}
for(i=start,j=0;j<MAX;i++,j++)
{
cout<<str[i];
}
cout<<endl;
}
return 0;
}