POJ3974 Palindrome
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。
本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!
Description
A string is said to be a palindrome if it reads the same both forwards and backwards, for example "madam" is a palindrome while "acm" is not.
The students recognized that this is a classical problem but couldn't come up with a solution better than iterating over all substrings and checking whether they are palindrome or not, obviously this algorithm is not efficient at all, after a while Andy raised his hand and said "Okay, I've a better algorithm" and before he starts to explain his idea he stopped for a moment and then said "Well, I've an even better algorithm!".
If you think you know Andy's final solution then prove it! Given a string of at most 1000000 characters find and print the length of the largest palindrome inside this string.
Input
Output
Sample Input
abcbabcbabcba
abacacbaaaab
END
Sample Output
Case 1: 13
Case 2: 6
正解:manacher
解题报告:
manacher模板题。
网上有一篇博客写得很清楚:https://segmentfault.com/a/1190000003914228。
大致做法就是:维护一个目前所接触的最右端(不妨设为$maxRight$),和使得最长回文子串取到最右端的对称中心位置(不妨设为$id$),我们需要对于每个i求$p[i]$,$p[i]$表示以$i$为中心的最长回文串半径(包括$i$自身)。
每次处理i时,分类讨论:
如果$i>=maxRight$,显然$i$只能重新开始拓展,不能利用前面的结果,暴力比较即可;
如果$i<maxRight$,又可以分两种情况讨论,那篇博客里面说的很清楚了,分为$p[j]$很长和$p[j]$很短两种情况——总之就是可以利用之前的对于现在的$p[i]$确定一个初值,即$p[i]=min(p[2*id-i],maxRight-i)$。
容易发现最后$max(p[i]-1)$就是我们想求的答案。
//It is made by ljh2000
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
const int MAXN = 2000011;
const int MAXM = 1000011;
int len,n,p[MAXN],Case,ans,id,maxRight;
char ch[MAXM],s[MAXN]; inline int getint(){
int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
} inline void manacher(){
s[0]='%'; s[1]='#'; n=1;
for(int i=0;i<len;i++) {
s[++n]=ch[i];
s[++n]='#';//插入字符,化偶为奇
}
maxRight=0; id=0;
for(int i=1;i<=n;i++) {
//p[i]表示以i为中心的最长回文串半径(包括i)
if(i<maxRight) p[i]=min(p[2*id-i],maxRight-i); else p[i]=1;
for(;i+p[i]<=n && s[i-p[i]]==s[i+p[i]];p[i]++) ;
if(i+p[i]>maxRight) { id=i; maxRight=i+p[i]; }
}
for(int i=1;i<=n;i++) ans=max(ans,p[i]);
ans--;
} inline void work(){
while(scanf("%s",ch)!=EOF) {
len=strlen(ch); if(len==3 && ch[0]=='E' && ch[1]=='N' && ch[2]=='D') break;
memset(p,0,sizeof(p)); ans=1;
manacher(); Case++;
printf("Case %d: %d\n",Case,ans);
}
} int main()
{
work();
return 0;
}
POJ3974 Palindrome的更多相关文章
- POJ----(3974 )Palindrome [最长回文串]
Time Limit: 15000MS Memory Limit: 65536K Total Submissions: 5121 Accepted: 1834 Description Andy ...
- poj3974 Palindrome【回文】【Hash】【二分】
Palindrome Time Limit: 15000MS Memory Limit: 65536K Total Submissions: 13157 Accepted: 5028 Desc ...
- POJ3974 Palindrome (manacher算法)
题目大意就是说在给定的字符串里找出一个长度最大的回文子串. 才开始接触到manacher,不过这个算法的确很强大,这里转载了一篇有关manacher算法的讲解,可以去看看:地址 神器: #includ ...
- POJ--3974 Palindrome(回文串,hash)
链接:点击这里 #include<iostream> #include<algorithm> #include<stdio.h> #include<cstri ...
- 【Manacher算法】poj3974 Palindrome
Manacher算法教程:http://blog.csdn.net/ggggiqnypgjg/article/details/6645824 模板题,Code 附带注释: #include<cs ...
- 【后缀数组】【线段树】poj3974 Palindrome
考虑奇数长度的回文,对于字符串上的每个位置i,如果知道从i开始的后缀和到i为止的前缀反转后的字符串的lcp长度的话,也就知道了以第i个字符为对称中心的最长回文的长度了.因此,我们用在S中不会出现的字符 ...
- POJ3974 Palindrome Manacher 最长回文子串模板
这道题可以$O(nlogn)$,当然也可以$O(n)$做啦$qwq$ $O(nlogn)$的思路是枚举每个回文中心,通过哈希预处理出前缀和后缀哈希值备用,然后二分回文串的长度,具体的就是判断在长度范围 ...
- [POJ3974]Palindrome(后缀数组 || manacher)
传送门 求一个串的最长回文子串的长度 1.后缀数组 把这个串反转后接到原串的后面,中间连一个没有出现过的字符. 然后求这个新字符串的某两个后缀的公共前缀的最大值即可. ——代码 #include &l ...
- [poj3974] Palindrome 解题报告 (hash\manacher)
题目链接:http://poj.org/problem?id=3974 题目: 多组询问,每组给出一个字符串,求该字符串最长回文串的长度 数据范围支持$O(nlog n)$ 解法一: 二分+hash ...
随机推荐
- c程序设计语言_习题1-19_编写函数reverse(s)将字符串s中字符顺序颠倒过来。
Write a function reverse(s) that reverses the character string s . Use it to write a program that re ...
- JavaScript里的类和继承
JavaScript与大部分客户端语言有几点明显的不同: JS是 动态解释性语言,没有编译过程,它在程序运行过程中被逐行解释执行JS是 弱类型语言,它的变量没有严格类型限制JS是面向对象语言,但 没有 ...
- [Android]在Dagger 2中Activities和Subcomponents的多绑定(翻译)
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/6266442.html 在Dagger 2中Activities ...
- Linux中的模式转换
模式转换: 编辑-->输入: i: 在当前光标所在字符的前面,转为输入模式: a: 在当前光标所在字符的后面,转为输入模式: o: 在当前光标所在行的下方,新建一行,并转为输入模式: I:在当前 ...
- nodejs调试工具node-inspector入门随笔
最近打算玩玩node. 众所周知,在前端,调试代码有一众天然好工具——浏览器!特别是 chrome,使得 jser 们如鱼得水,玩得风生水起.但是到了node,情况就不一样了,js 代码不再运行在单纯 ...
- 洛谷P1189 逃跑的拉尔夫(SEARCH)
洛谷1189 SEARCH 题目描述 年轻的拉尔夫开玩笑地从一个小镇上偷走了一辆车,但他没想到的是那辆车属于警察局,并且车上装有用于发射车子移动路线的装置. 那个装置太旧了,以至于只能发射关于那辆车的 ...
- JAVA面试题——JAVA编程题1(2015.07.22——湛耀)
实现代码很简单: package com.xiaozan.shopping; import java.util.Arrays; public class ShoppingCart { ...
- Hive 0.12 Caused by: MetaException(message:Version information not found in metastore. )解决方法
配置完成Mysql存储元数据信息,启动后测试show tables报错ERROR exec.DDLTask: org.apache.hadoop.hive.ql.metadata.HiveExcept ...
- ndk-gdb of NDK r9d modified to *always* debug the ":remote"-process of your app
https://gist.github.com/TomTasche/9690186 ndk-gdb of NDK r9d modified to *always* debug the ":r ...
- 再看GOPATH
原本不打算介绍GOPATH,然而,总是有初学者问一些关于GOPATH的问题,因此在这里再介绍一下GOPATH GOPATH环境变量用于指定这样一些目录:除$GOROOT之外的包含Go项目源代码和二进制 ...