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. Python开发【第二十篇】:缓存

    Python开发[第二十篇]:缓存redis&Memcache   点击这里 Python之路[第九篇]:Python操作 RabbitMQ.Redis.Memcache.SQLAlchemy ...

  2. Android 实现闹钟功能

      原文地址:Android 实现闹钟功能作者:Android_Learners 一.手机闹钟主要用到了AlarmManager类,AlarmManager类提供了访问系统定时服务的途径,开发人员可以 ...

  3. 动态拼接 sql的时候 里面 如果有变量的话 按上面的方式进行处理

    set @Sql_Sql = N' select top 1 @m_zw=zw,@m_zh=temp from ket where zd=''ddd'' ' print @Sql_Sql EXEC s ...

  4. Java 最简单的批处理

    批处理Batch && PreparedStatement : import java.sql.*; public class TestBatch { public static vo ...

  5. 那些年,我们一起学WCF--(8)Single实例行为

    Single实例行为,类似于单件设计模式,所有可以客户端共享一个服务实例,这个服务实例是一个全局变量,该实例第一次被调用的时候初始化,到服务器关闭的时候停止. 设置服务为Single实例行为,只要设置 ...

  6. 关于ASP.NET控件方面的学习(恢复版)

    前段时间没有把学习中的遇到的问题和解决方法详细总结,今天整理整理.. 鉴于我们这个研究生论文管理系统是小组形式,所以说虽然我只负责数据库,但是其它部分也多少有些工作方面的涉及,最后感谢各位同学和老师的 ...

  7. iOS 小知识 - #if , #ifdef , #ifndef.

    Q : 在项目的 .h 文件中, #ifndef XXX_h #define XXX_h //程序段1 #endif  /* XXX_h */ 的作用? A : 如果 XXX.h 不存在,就引入 XX ...

  8. Asp.Net 注册 邮箱激活

    数据库 表的设计 State为用户状态  0为禁用  1为可用  默认为0,下面有个UserGUID,这个字段将来用于激活账户 首先你要写一个表单,验证码神马的,这个我就不写了..直接写处理的 代码在 ...

  9. hdoj 1087 (DP)

    代码: #include<iostream>   #include<cmath>   using namespace std;  int a[1005], dp[1005];  ...

  10. 1. mybatis批量插入数据

    通过list <insert id="saveByList" useGeneratedKeys="true" parameterType="ja ...