U - Palindrome Manacher
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
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<string>
#include<map>
#include<cstring>
#include<fstream>
using namespace std;
#define MAXN 1000003
typedef long long LL;
/*
最长回文串的长度
*/
char a[MAXN];
char s[MAXN*];
int r[MAXN*];
void Manacher(int len)
{
int p=;
s[p++] = '$';
s[p++] = '#';
for(int i=;i<len;i++)
{
s[p++] = a[i];
s[p++] = '#';
}
s[p] = ; int Mx = ,pos = ;
for(int i=;i<p;i++)
{
r[i] = (i<Mx)? min(Mx-i,r[*pos-i]):;
while(s[i+r[i]]==s[i-r[i]])
r[i]++;
if(i+r[i]>Mx)
{
Mx = i+r[i];
pos = i;
}
}
}
int main()
{
int cas = ;
while(scanf("%s",a),a[]!='E')
{
int l = strlen(a);
Manacher(l);
int ans = ;
for(int i=;i<*l+;i++)
{
ans = max(ans,r[i]-);
}
printf("Case %d: %d\n",cas++,ans);
}
}
U - Palindrome Manacher的更多相关文章
- hdu6230 Palindrome(manacher+树状数组)
题目链接: Palindrome Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Other ...
- poj 3974 Palindrome (manacher)
Palindrome Time Limit: 15000MS Memory Limit: 65536K Total Submissions: 12616 Accepted: 4769 Desc ...
- POJ3974 Palindrome (manacher算法)
题目大意就是说在给定的字符串里找出一个长度最大的回文子串. 才开始接触到manacher,不过这个算法的确很强大,这里转载了一篇有关manacher算法的讲解,可以去看看:地址 神器: #includ ...
- ural 1297 Palindrome(Manacher模板题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 求最长回文子串. http://acm.timus.ru/problem.aspx ...
- ●POJ 3974 Palindrome(Manacher)
题链: http://poj.org/problem?id=3974 题解: Manacher 求最长回文串长度. 终于会了传说中的马拉车,激动.推荐一个很棒的博客:https://www.61mon ...
- HDU 3856 Palindrome ( Manacher + RMQ + 二分 ) WA!!!
不知道错在哪了,求大神指教!!! 思路:用manacher求出每个以str[i]为中心轴的回文串的长度,RMQ预处理区间最大值,对于每个查询,二分最大回文串长,判定是否可行. #include < ...
- POJ3974 Palindrome Manacher 最长回文子串模板
这道题可以$O(nlogn)$,当然也可以$O(n)$做啦$qwq$ $O(nlogn)$的思路是枚举每个回文中心,通过哈希预处理出前缀和后缀哈希值备用,然后二分回文串的长度,具体的就是判断在长度范围 ...
- Hdu-6230 2017CCPC-哈尔滨站 A.Palindrome Manacher 主席树
题面 题意:给你一个字符串,问你满足s[i]=s[2n-i]=s[2n+i-2]的子串(这子串长度为3n-2)有多少个,原字符串长度<=5e5 题解:对于这种子串,其实要满足2个回文,跑过一次M ...
- poj3974 Palindrome(Manacher最长回文)
之前用字符串hash+二分过了,今天刚看了manacher拿来试一试. 这manacher也快太多了%%% #include <iostream> #include <cstring ...
随机推荐
- 支持HTTP2的cURL——基于Alpine的最小化Docker镜像
cURL是我喜欢的开源软件之一.虽然cURL的强大常常被认为是理所当然的,但我真心地认为它值得感谢和尊重.如果我们的工具箱失去了curl,那些需要和网络重度交互的人(我们大多数人都是这样的)将会陷入到 ...
- PCB InCAM 获取 JOB STEP 实现外挂脚本调试功能实现
PCB CAM自动化基于Incam 打造,在测试时经常遇到调试障碍,每次自行对功能测试时,生成了exe脚本后,再到Incam里面运行,发现问题,再回来修改代码,非常不爽, 参考Genesis调试运行模 ...
- BZOJ 1137 半平面交
半平面交的板子 //By SiriusRen #include <bits/stdc++.h> #define double long double using namespace std ...
- hdu2027
http://acm.hdu.edu.cn/showproblem.php?pid=2027 #include<iostream> #include<stdio.h> #inc ...
- Android内存管理(13)常见产生内存泄漏的原因
1.集合类泄漏 集合类如果仅仅有添加元素的方法,而没有相应的删除机制,导致内存被占用.如果这个集合类是全局性的变量 (比如类中的静态属性,全局性的 map 等即有静态引用或 final 一直指向它), ...
- 327 Count of Range Sum 区间和计数
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- Zookeeper概念学习系列之zookeeper的角色
详细,见如下图 1.领导者(leader) : 负责进行投票的发起和决议,更新系统状态. 2.学习者(learner): 包括跟随者(follower)和观察者(observer). 跟随者(foll ...
- ZUK 22(Z2131) 免解锁BL 免rec 保留数据 Magisk Xposed 救砖 ROOT ZUI 4.0.199
>>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...
- JS——AJAX
向服务器发送请求如需将请求发送到服务器,我们使用 XMLHttpRequest 对象的 open() 和 send() 方法:Open方法了有两种请求方式——get和post 与 POST 相比,GE ...
- 【技术累积】【点】【java】【22】UUID
基础概念&使用 UUID是Universally Unique Identifier的缩写,它是在一定的范围内(从特定的名字空间到全球)唯一的机器生成的标识符. 说白了就是个唯一键,只不过到处 ...