Palindrome(Manacher)
Time Limit: 15000MS | Memory Limit: 65536K | |
Total Submissions: 6183 | Accepted: 2270 |
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
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
Sample Input
abcbabcbabcba
abacacbaaaab
END
Sample Output
Case 1: 13
Case 2: 6
manacher;
代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
const int MAXN=;
char str[MAXN],s[MAXN<<];
int p[MAXN<<];
int Manacher(char *s,int len){
p[]=p[]=;
int ans=,id=,mx=;
for(int i=;i<len;i++){
if(mx>i)p[i]=min(p[*id-i],mx-i);
else p[i]=;
while(s[i-p[i]]==s[i+p[i]])p[i]++;
if(p[i]+i>mx)mx=p[i]+i,id=i;
ans=max(ans,p[i]-);
}
return ans;
}
int main(){
int flot=;
while(~scanf("%s",str),strcmp(str,"END")){
int len=strlen(str);
s[]='@';
for(int i=;i<len;i++){
s[*i+]='#';
s[*i+]=str[i];
}
s[*len+]='#';
printf("Case %d: %d\n",++flot,Manacher(s,*len+));
}
return ;
}
Palindrome(Manacher)的更多相关文章
- URAL 1297 Palindrome(Manacher)
The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent ...
- 【学习笔记】字符串—马拉车(Manacher)
[学习笔记]字符串-马拉车(Manacher) 一:[前言] 马拉车用于求解连续回文子串问题,效率极高. 其核心思想与 \(kmp\) 类似:继承. --引自 \(yyx\) 学姐 二:[算法原理] ...
- O(n)回文子串(Manacher)算法
O(n)回文子串(Manacher)算法 资料来源网络 参见:http://www.felix021.com/blog/read.php?2040 问题描述: 输入一个字符串,求出其中最大的回文子串. ...
- HDU 4513 吉哥系列故事——完美队形II(Manacher)
Problem Description 吉哥又想出了一个新的完美队形游戏! 假设有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希望从中挑出一些人,让这些人形成 ...
- 牛客小白月赛13 小A的回文串(Manacher)
链接:https://ac.nowcoder.com/acm/contest/549/B来源:牛客网 题目描述 小A非常喜欢回文串,当然我们都知道回文串这种情况是非常特殊的.所以小A只想知道给定的一个 ...
- 2018.06.30 BZOJ 2342: [Shoi2011]双倍回文(manacher)
2342: [Shoi2011]双倍回文 Time Limit: 10 Sec Memory Limit: 128 MB Description Input 输入分为两行,第一行为一个整数,表示字符串 ...
- Palindrome(poj3974)(manacher算法)
http://poj.org/problem?id=3974 Palindrome Time Limit: 15000MSMemory Limit: 65536K Total Submissions: ...
- POJ3974:Palindrome(Manacher模板)
Palindrome Time Limit: 15000MS Memory Limit: 65536K Total Submissions: 14021 Accepted: 5374 题目链接 ...
- 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 ...
随机推荐
- vs2013 中HTML页 无法在设计窗口中查看的解决
VS2013不支持HTML文件的解决办法: 1.将html文件重命名为aspx即可.不需要创建项目,直接拖进vs即可. 2.打开VS菜单->工具->选项->文本编辑器->文件扩 ...
- linq中的GroupBy总结
1.简单形式: var q = from p in db.Products group p by p.CategoryID into g select g; 语句描述:Linq使用Group By按C ...
- JavaSE复习日记 : 递归函数
/* * 递归函数 * 什么是递归? * 在一个方法的内部,对自身进行调用,又叫做递归调用 * * 递归和循环的编写都包括三部分: * 1. 初始值; * 2. 终止条件; * 3. 前进步长; * ...
- springmvc的ModelAndView的简单使用
参考:http://blog.csdn.net/zzjjiandan/article/details/34089313 先上图: MAVTest.java package com.wyl; impor ...
- win7系统中桌面图标显示不正常问题
http://jingyan.baidu.com/article/466506580c9327f549e5f8dc.html 最近笔者在安装软件时,突然出现了桌面图标显示不正常了,一开始还以为是新安装 ...
- MySQL、You are using safe update mode
MySQL默认模式是sql_safe_updates=1的.在这个模式下不管你是update还是delete一行where 条件都要指定主键.如果where条件只出现的不是主键就会出错. 例子: se ...
- PCB成型製程介紹
PCB成型製程在電子構裝中所扮演的角色 下圖是電腦主機的內部組成 我們將以插在主機板上的一片 USB擴充卡來說明PCB成型製 程在電子構裝中所扮演的角色 PCB成型製程的子製程 USB擴充卡要插入主機 ...
- 7篇Model View和4篇双缓冲
http://www.cnblogs.com/SkylineSoft/category/299475.html
- Advanced Customization of the jQuery Mobile Buttons | Appcropolis
Advanced Customization of the jQuery Mobile Buttons | Appcropolis Advanced Customization of the jQue ...
- cocos2d-x 多点触控实现缩放及相关问题的解决方法
首先,来看下代码: 声明文件: #ifndef __loading__MoreTouches__ #define __loading__MoreTouches__ #include <iostr ...