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之路【第十四篇】:AngularJS --暂无内容-待更新

    Python之路[第十四篇]:AngularJS --暂无内容-待更新

  2. titlebar和actionbar上的按钮设置

    ---恢复内容开始--- Actionbar加按钮: 在res文件夹下新建menu文件夹(如果你没有),然后添加一个XML文件 <?xml version="1.0" enc ...

  3. Rouh set 入门知识1(基础定义篇)

    粗糙集理论是继概率论.模糊集.证据论后又一处理不完整性和不确定性的数学工具,建立在分类机制的基础上.无需提供问题所处理的数据集合之外的任何先验信息条件.并且能有效分析不精确.不一致.不完整等各种不完备 ...

  4. ES6数组去重

    今天五一,在出去玩之前赶紧写篇博客,时刻不要忘记学习^_^!! 提到数组去重,想必大家都不陌生,会的同学可能噼里啪啦写出好几个,下面来看看之前常见的去重代码: 'use strict'; var ar ...

  5. weex-toolkit打包

    需要2.15.1及以上的npm支持 运行终端,查看版本 npm --version 如果版本低于2.15.1,需要更新 sudo npm install -g npm 安装weex-toolkit n ...

  6. java中的类集框架

    1.什么是类集框架 1.是一组类和接口 2.位于java.util包当中 3.主要用于用户存储和管理对象 4.主要分为三大类——集合.列表和映射 2.类集框架图 虚线框的表示接口,实线框的表示实现类 ...

  7. PHP 开发API接口 注册,登录,查询用户资料

    服务端 <?php require 'conn.php'; header('Content-Type:text/html;charset=utf-8'); $action = $_GET['ac ...

  8. maven+jetty项目在tomcat部署

    步骤1:项目打包 clean install 步骤二:拷贝war 包到tomcat下 步骤三:修改server.xml文件的端口 步骤四:启动tomcat,注意jetty的项目是不需要带项目名的,To ...

  9. python sklearn模型的保存

    使用python的机器学习包sklearn的时候,如果训练集是固定的,我们往往想要将一次训练的模型结果保存起来,以便下一次使用,这样能够避免每次运行时都要重新训练模型时的麻烦. 在python里面,有 ...

  10. 80端口被占用 PID = 4解决办法

    请按照下面的步骤来运行命令:1. sc config http stat = demand2. reboot3. run the command(netsh http show servicestat ...