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 ;
}

Manacher Ural 1297 Palindrome的更多相关文章

  1. Ural 1297 Palindrome(Manacher或者后缀数组+RMQ-ST)

    1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The “U.S. Robots” HQ has just received a ...

  2. URAL 1297 Palindrome 后缀数组

    D - Palindrome Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Subm ...

  3. 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome

    题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...

  4. URAL - 1297 Palindrome —— 后缀数组 最长回文子串

    题目链接:https://vjudge.net/problem/URAL-1297 1297. Palindrome Time limit: 1.0 secondMemory limit: 64 MB ...

  5. ural 1297 Palindrome(Manacher模板题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 求最长回文子串. http://acm.timus.ru/problem.aspx ...

  6. URAL 1297 Palindrome(Manacher)

    The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent ...

  7. URAL 1297 Palindrome 最长回文子串

    POJ上的,ZOJ上的OJ的最长回文子串数据量太大,用后缀数组的方法非常吃力,所以只能挑个数据量小点的试下,真要做可能还是得用manacher.贴一下代码 两个小错,一个是没弄懂string类的sub ...

  8. URAL 1297 Palindrome(后缀数组+ST表)

    [题目链接] http://acm.timus.ru/problem.aspx?num=1297 [题目大意] 求最长回文子串,并输出这个串. [题解] 我们将原串倒置得到一个新的串,加一个拼接符将新 ...

  9. Ural 1297 Palindrome 【最长回文子串】

    最长回文子串 相关资料: 1.暴力法 2.动态规划 3.中心扩展 4.Manacher法 http://blog.csdn.net/ywhorizen/article/details/6629268 ...

随机推荐

  1. inner join

    select Person.LastName,Person.FirstName,Orders.OrderNo from Persons INNER JOIN Orders ON Person.Id_P ...

  2. OBJECT和EMBED标签(转载)

    一.介绍: 我们要在网页中正常显示flash内容,那么页面中必须要有指定flash路径的标 签.也就是OBJECT和EMBED标签.OBJECT标签是用于windows平台的IE浏览器的,而EMBED ...

  3. equals函数的作用

    1.equals函数在什么地方 在Object类中,写法与==一样,但是我们用的时候要重写这个equals方法 String类型中的equals是复写好的 2.equals函数的作用 ==号在比较两个 ...

  4. Android开发常见错误类型一览表

    这是我的第一个博客,我会一直添加我在Android开发中遇到的错误,用来记录我开发中踩过的那些坑 ------------------------分割线------------------------ ...

  5. VSS Admin 清除密码

    [参阅链接]http://www.cnblogs.com/Zealot/archive/2004/09/18/44309.html the secret is to hack the um.dat f ...

  6. 一则自用iptables例子解释

    公网IP:110.24.3.83内网IP:10.252.214.186局域网数据库:10.252.214.100 通过NAT端口转发,访问110.24.3.83:3308端口跳转到局域网数据库机器的3 ...

  7. 在xcode6.1和ios10.10.1环境下实现app发布

    之前写过在xcode6.1和ios10.10.1环境下实现真机测试,以及最近提交的app一直在审核当中,所以木有发布如何实现app发布来分享给大家.刚好昨天app审核通过了,所以就分享一篇如何实现ap ...

  8. 【转】 UITableViewCell的标记、移动、删除、插入

    原文: http://blog.csdn.net/duxinfeng2010/article/details/7725897 这篇文章是建立在 代码实现 UITableView与UITableView ...

  9. style currentStyle getComputedStyle的区别和用法

    先介绍下层叠样式表的三种形式: 1.内联样式,在html标签中style属性设置. <p style="color:#f90">内联样式</p> 2.嵌入样 ...

  10. 计算机网络基础_01IP地址

    1,IP地址组成和分级分级 IP地址=网络地址+主机地址 32位,4段组成 A:最高位是0 ,1个字节的网络地址,3个字节的主机地址 B:最高位是10,2个字节的网络地址,2个字节的主机地址 C:最高 ...