Manacher Ural 1297 Palindrome
1297. Palindrome
Memory limit: 64 MB
“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).
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.
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.
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
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
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的更多相关文章
- 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 ...
- URAL 1297 Palindrome 后缀数组
D - Palindrome Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Subm ...
- 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome
题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...
- URAL - 1297 Palindrome —— 后缀数组 最长回文子串
题目链接:https://vjudge.net/problem/URAL-1297 1297. Palindrome Time limit: 1.0 secondMemory limit: 64 MB ...
- ural 1297 Palindrome(Manacher模板题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 求最长回文子串. http://acm.timus.ru/problem.aspx ...
- URAL 1297 Palindrome(Manacher)
The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent ...
- URAL 1297 Palindrome 最长回文子串
POJ上的,ZOJ上的OJ的最长回文子串数据量太大,用后缀数组的方法非常吃力,所以只能挑个数据量小点的试下,真要做可能还是得用manacher.贴一下代码 两个小错,一个是没弄懂string类的sub ...
- URAL 1297 Palindrome(后缀数组+ST表)
[题目链接] http://acm.timus.ru/problem.aspx?num=1297 [题目大意] 求最长回文子串,并输出这个串. [题解] 我们将原串倒置得到一个新的串,加一个拼接符将新 ...
- Ural 1297 Palindrome 【最长回文子串】
最长回文子串 相关资料: 1.暴力法 2.动态规划 3.中心扩展 4.Manacher法 http://blog.csdn.net/ywhorizen/article/details/6629268 ...
随机推荐
- junit 测试注解
* @Test: 将一个 普通的方法修饰成为一个测试方法* @BeforeClass: 他会在所有的方法运行前被执行,static修饰* @AfterClass 他会在所有方法运行结束后被执行,sta ...
- react初识
如下是在研究中记录的笔记: 1,作用:局部的更新dom结构;虚拟dom保证性能2,和mvc不同,mvc是对于技术上的分离(分类),而react是组件上的分离,每个视图模块分离,复用,以视图模块为单位3 ...
- Gprinter Android SDK V2.1.4 使用说明
佳博打印机Android的SDK开发包,已更新到Gprinter Android SDK V2.1.4. IOS的SDK开发包更新为GprinterSDKandDemoforIOS_v1.0.8. 根 ...
- FluentNHibernate当数据库设置默认值时,使用插入操作,导致默认值没有写入问题
需要再映射属性字段增加Not.Insert() Map(x => x.Provrince, "PROVRINCE").Not.Insert(); Map(x => x. ...
- I/O多路转接之select
系统提供select函数来实现多路复⽤用输入/输出模型.select系统调用是用来让我们的程序监视 多个文件句柄的状态变化的.程序会停在select这里等待,直到被监视的文件句柄有一个或 多个发生了状 ...
- CentOS Device eth0 does not seem to be present 解决方案
1. vi /etc/udev/rules.d/70-persistent-net.rules 记录eth1 的网卡地址 2.vi /etc/sysconfig/network-scrpits/ifc ...
- spring配置中引入properties
<context:property-placeholder location="classpath*:db.properties" />
- URPF 简单流程
主要功能是防止基于源地址欺骗的网络攻击. 路由器接口一旦使能URPF功能,当该接口收到数据报文时,首先会对数据报文的源地址进行合法性检查,对于源地址合法性检查通过的报文,才会进一步查找去往目的地址的转 ...
- 手机端QQ客服直接跳转到QQ
企业QQ呼出QQ对话框方法 1.手机端链接是这样的:mqqwpa://im/chat?chat_type=wpa&uin=386807630&version=1&src_typ ...
- WdatePicker.js 日期时间插件
支持功能: 1.支持常规在input单击或获得焦点时调用,还支持使用其他的元素如:<img><div>等触发WdatePicker函数来调用弹出日期框 @1.input 调用: ...