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模板)
题意:就是求一个串的最长回文子串....输出长度. 直接上代码吧,没什么好分析的了. 代码如下: ================================================= ...
随机推荐
- 【button】 按钮组件说明
原型: <button size="[default | mini]" type="[primary | default | warn]" plain=& ...
- leetcode-最长上升子序列LIS
转载原文地址:http://www.cnblogs.com/GodA/p/5180560.html 给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7, ...
- 150命令之线上查询及帮助命令 man hellp
150命令之线上查询及帮助命令 man 查询命令的帮助 man + 命令 NAME ls - list directory contents 命令+命令简单说明 SYNOPSIS ...
- 从hive导入到oracle(Hcatalog)
1.使用catalog的情况下: sqoop export --table tableName2 \ #oracle表 --connect jdbc:oracle:thin:@127.0.0.1:15 ...
- HDU 3265/POJ 3832 Posters(扫描线+线段树)(2009 Asia Ningbo Regional)
Description Ted has a new house with a huge window. In this big summer, Ted decides to decorate the ...
- 团队作业week9 情景测试
一.使用人群:学生.计算机工作者.对计算机感兴趣的人 1.学生:学生是学霸系统的主要用户.学生一般会通过网络寻找与自己的课程,作业有关的信息.首先,可以通过我们的搜索功能在我们的数据库中寻找我们从网络 ...
- android入门 — AlertDialog对话框
常见的对话框主要分为消息提示对话框.确认对话框.列表对话框.单选对话框.多选对话框和自定义对话框. 对话框可以阻碍当前的UI线程,常用于退出确认等方面. 在这里主要的步骤可以总结为: 1.创建Aler ...
- js 拼接字符串时,本来想要’#1′ ,返回的却是’#01′
今天在操作一个元素时,id值是拼接的. var index = $(this).attr(‘index’); //0var id = ‘#’ + (index+1); //#01$(id) ...
- 《剑指offer》---两个栈实现队列
本文算法使用python3实现 1.题目描述: 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 时间限制:1s:空间限制:32768K 2.思路描述: ...
- 按Backspace键删除时,会出现^H
按Backspace键删除时,会出现^H 2014-08-12 19:38 1180人阅读 评论(0) 举报 版权声明:本文为博主原创文章,未经博主允许不得转载. 在linux/unix 平台的经常使 ...