Manacher Ural 1297 Palindrome

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

  这题用马拉车算法,模板题。

 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=;
char s[maxn],S[maxn];
int maxl[maxn],len;
int main()
{
int l=;
scanf("%s",s);
len=strlen(s);
S[l++]='$';
S[l++]='&';
for(int i=;i<len;i++)
S[l++]=s[i],S[l++]='&';
S[l]=;
int id=,mp=;
for(int i=;i<l;i++){
maxl[i]=mp>i?min(mp-i,maxl[id*-i]):;
while(S[i+maxl[i]]==S[i-maxl[i]])maxl[i]++;
if(maxl[i]+i>mp){
mp=maxl[i]+i;
id=i;
}
}
int ans=;
for(int i=;i<=l;i++){
if(maxl[i]>ans)
ans=maxl[i],id=i;
}
for(int i=id-ans+;i<id+ans;i++)
if(S[i]!='&')
printf("%c",S[i]);
printf("\n");
}

  还有后缀数组的解法

 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=;
char s[maxn];
int r[maxn],Wa[maxn],Wb[maxn],Wv[maxn],Ws[maxn];
int sa[maxn],rank[maxn],lcp[maxn],len;
bool cmp(int *p,int a,int b,int l){
return p[a]==p[b]&&p[a+l]==p[b+l];
}
void DA(int n,int m){
int i,j,p,*x=Wa,*y=Wb,*t;
for(i=;i<m;i++)Ws[i]=;
for(i=;i<n;i++)++Ws[x[i]=r[i]];
for(i=;i<m;i++)Ws[i]+=Ws[i-];
for(i=n-;i>=;i--)sa[--Ws[x[i]]]=i; for(j=,p=;p<n;m=p,j<<=){
for(i=n-j,p=;i<n;i++)
y[p++]=i;
for(i=;i<n;i++)
if(sa[i]>=j)
y[p++]=sa[i]-j;
for(i=;i<m;i++)Ws[i]=;
for(i=;i<n;i++)++Ws[Wv[i]=x[y[i]]];
for(i=;i<m;i++)Ws[i]+=Ws[i-];
for(i=n-;i>=;i--)sa[--Ws[Wv[i]]]=y[i];
for(t=x,x=y,y=t,x[sa[]]=,p=,i=;i<n;i++)
x[sa[i]]=cmp(y,sa[i],sa[i-],j)?p-:p++;
}
} void Lcp(int n){
int i,j,k=;
for(i=;i<=n;i++)
rank[sa[i]]=i;
for(i=;i<n;lcp[rank[i++]]=k)
for(k?--k:k,j=sa[rank[i]-];r[i+k]==r[j+k];k++);
} int mm[maxn],Min[maxn][]; void Make_ST(int n){
mm[]=-;
for(int i=;i<=n;i++){
mm[i]=(i&(i-))?mm[i-]:mm[i-]+;
Min[i][]=lcp[i];
}
for(int k=;k<=mm[n];k++)
for(int i=;i+(<<k)-<=n;i++)
Min[i][k]=min(Min[i][k-],Min[i+(<<(k-))][k-]);
return;
} int Query(int a,int b){
if(a>b)swap(a,b);a++;
return min(Min[a][mm[b-a+]],Min[b-(<<mm[b-a+])+][mm[b-a+]]);
} int main(){
scanf("%s",s);
len=strlen(s);
s[len]='$';
for(int i=len+;i<=*len;i++)
s[i]=s[len*-i];
len=len*+;
for(int i=;i<len;i++)
r[i]=s[i];
DA(len+,);
Lcp(len);
Make_ST(len);
int ans=-,pos,ret;
for(int i=;i<len/;i++){
ret=Query(rank[i],rank[len-i-]);//长度为奇数
if(ret*->ans){
ans=ret*-;
pos=i-ret+;
}
ret=Query(rank[i],rank[len-i]);//长度为偶数
if(ret*>ans){
ans=ret*;
pos=i-ret;
}
} for(int i=;i<ans;i++)
printf("%c",s[i+pos]); printf("\n");
return ;
}
上一篇:C# lambda


下一篇:直接引用windows命名空间