POJ3974:Palindrome(Manacher模板)
Palindrome
| Time Limit: 15000MS | Memory Limit: 65536K | |
| Total Submissions: 14021 | Accepted: 5374 |
题目链接:http://poj.org/problem?id=3974
Description:
Andy the smart computer science student was attending an algorithms class when the professor asked the students a simple question, "Can you propose an efficient algorithm to find the length of the largest palindrome in a string?"
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:
Your program will be tested on at most 30 test cases, each test case is given as a string of at most 1000000 lowercase characters on a line by itself. The input is terminated by a line that starts with the string "END" (quotes for clarity).
Output:
For each test case in the input print the test case number and the length of the largest palindrome.
Sample Input:
abcbabcbabcba
abacacbaaaab
END
Sample Output:
Case 1: 13
Case 2: 6
题意:
求最长回文串的长度。
题解:
直接马拉车算法套下就行了。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
typedef long long ll;
const int N = ;
char s[N],tmp[N];;
int p[N];
void Manacher(char *s){
memset(p,,sizeof(p));
int l=strlen(s);
strcpy(tmp,s);
s[]='$';
for(int i=;i<=*l+;i++){
if(i&) s[i]='#';
else s[i]=tmp[i/-];
}
s[*l+]='\0';
int mx=,id=;
l=strlen(s);
for(int i=;i<l;i++){
if(i>=mx) p[i]=;
else p[i]=min(mx-i,p[*id-i]);
while(s[i-p[i]]==s[i+p[i]]) p[i]++;
if(p[i]+i>mx){
mx=p[i]+i;
id=i;
}
}
}
int main(){
int cnt = ;
while(scanf("%s",s)!=EOF){
cnt++;
if(s[]=='E'&&s[]=='N') break;
Manacher(s);
int ans = ;
int l=strlen(s);
for(int i=;i<l;i++) ans=max(ans,p[i]-);
printf("Case %d: ",cnt);
printf("%d\n",ans);
}
return ;
}
POJ3974:Palindrome(Manacher模板)的更多相关文章
- ural 1297 Palindrome(Manacher模板题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 求最长回文子串. http://acm.timus.ru/problem.aspx ...
- POJ3974 Palindrome Manacher 最长回文子串模板
这道题可以$O(nlogn)$,当然也可以$O(n)$做啦$qwq$ $O(nlogn)$的思路是枚举每个回文中心,通过哈希预处理出前缀和后缀哈希值备用,然后二分回文串的长度,具体的就是判断在长度范围 ...
- POJ3974 Palindrome (manacher算法)
题目大意就是说在给定的字符串里找出一个长度最大的回文子串. 才开始接触到manacher,不过这个算法的确很强大,这里转载了一篇有关manacher算法的讲解,可以去看看:地址 神器: #includ ...
- poj3974 Palindrome(Manacher最长回文)
之前用字符串hash+二分过了,今天刚看了manacher拿来试一试. 这manacher也快太多了%%% #include <iostream> #include <cstring ...
- POJ3974 Palindrome
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- HDU 3068 最长回文(manacher模板题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3068 题目大意:求字符串s中最长的回文子串 解题思路:manacher模板 代码 #include&l ...
- HDU 3068 最长回文( Manacher模板题 )
链接:传送门 思路:Manacher模板题,寻找串中的最长回文子串 /***************************************************************** ...
- 【Manacher算法】poj3974 Palindrome
Manacher算法教程:http://blog.csdn.net/ggggiqnypgjg/article/details/6645824 模板题,Code 附带注释: #include<cs ...
- Palindrome - POJ 3974 (最长回文子串,Manacher模板)
题意:就是求一个串的最长回文子串....输出长度. 直接上代码吧,没什么好分析的了. 代码如下: ================================================= ...
随机推荐
- 最全的Markdown语法
目录 Markdown语法 多级标题 引用与注释 插入代码 行内代码 代码段 图片 超链接 行内超链接 参数式超链接 字体 表格 分割线 多级列表 无序列表 有序列表 多选框 LaTeX公式 行内La ...
- Python 学习笔记之——用 sklearn 对数据进行预处理
1. 标准化 标准化是为了让数据服从一个零均值和单位方差的标准正态分布.也即针对一个均值为 \(mean\) 标准差为 \(std\) 的向量 \(X\) 中的每个值 \(x\),有 \(x_{sca ...
- HADOOP (十一).安装hbase
下载安装包并解压设置hbase环境变量配置hbase-site.xml启动hbase检测hbase启动情况测试hbase shell 下载安装包并解压 https://mirrors.tuna.tsi ...
- Python3 数据类型-字典
字典是一种可变数据类型,且可存储任意类型对象. 字典使用大括号"{}"括起来,由键(key)和值(values)组成,键只能使用不可变类型定义,值可以使用可变类型{'键':'值'} ...
- JavaScript初探系列之面向对象
面向对象的语言有一个标志,即拥有类的概念,抽象实例对象的公共属性与方法,基于类可以创建任意多个实例对象,一般具有封装.继承.多态的特性!但JS中对象与纯面向对象语言中的对象是不同的,ECMA标准定义J ...
- lintcode-149-买卖股票的最佳时机
149-买卖股票的最佳时机 假设有一个数组,它的第i个元素是一支给定的股票在第i天的价格.如果你最多只允许完成一次交易(例如,一次买卖股票),设计一个算法来找出最大利润. 样例 给出一个数组样例 [3 ...
- PokeCats开发者日志(十)
现在是PokeCats游戏开发的第三十三天的中午,收到了中国版权保护中心软件登记部发来的受理通知书. 上易版权看一眼,貌似离拿证不远了. 想一想还有点小激动呢!
- intellij idea 如何将一个普通项目转换为maven项目
1.工程文件下新建文件pom.xml,并填写好内容. 2.在pom.xml 文件上右键 Add as Maven Project.
- 【Docker 命令】- inspect命令
docker inspect : 获取容器/镜像的元数据. 语法 docker inspect [OPTIONS] NAME|ID [NAME|ID...] OPTIONS说明: -f :指定返回值的 ...
- Linux命令之查看cpu个数_核数_内存总数
http://blog.csdn.net/cgwcgw_/article/details/10000053 cpu个数 cat /proc/cpuinfo | grep "physical ...