题目链接:http://poj.org/problem?id=3974

题意:求出给定字符串的最长回文串长度。

思路:裸的Manacher模板题。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN=+;
typedef long long int LL;
#define INF 0x3f3f3f3f
char str[MAXN],dstr[MAXN*];
int lenstr,lendstr,p[MAXN*],ans;
void manacher(){
memset(p,,sizeof(p));
int id=,mx=;
for(int i=;i<lendstr;i++){
if(mx>i){
p[i]=min(p[*id-i],mx-i);
}
else{
p[i]=;
}
while(dstr[i-p[i]]==dstr[i+p[i]]){
p[i]++;
}
if(p[i]+i>mx){
mx=p[i]+i;
id=i;
}
}
}
void init(){
dstr[]='$';
dstr[]='#';
for(int i=;i<lenstr;i++){
dstr[i*+]=str[i];
dstr[i*+]='#';
}
lendstr=lenstr*+;
dstr[lendstr]='*';
}
int main()
{
int Ca=;
while(~scanf("%s",str)){
if(strcmp(str,"END")==){
break;
}
lenstr=strlen(str);
init();
manacher();
ans=;
for(int i=;i<lendstr;i++){
ans=max(ans,p[i]);
}
printf("Case %d: %d\n",Ca++,ans-);
}
return ;
}

POJ 3974 回文串-Manacher的更多相关文章

  1. POJ 1159 回文串-LCS

    题目链接:http://poj.org/problem?id=1159 题意:给定一个长度为N的字符串.问你最少要添加多少个字符才能使它变成回文串. 思路:最少要添加的字符个数=原串长度-原串最长回文 ...

  2. BZOJ 2342 回文串-Manacher

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2342 思路:先跑一遍Manacher求出p[i]为每个位置为中心的回文半径,因为双倍回文串 ...

  3. BZOJ 2565 回文串-Manacher

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2565 题意:中文题 思路:定义L[i],R[i].表示以i为左端点/右端点时,最长回文串长 ...

  4. 【回文串-Manacher】

    Manacher算法能够在O(N)的时间复杂度内得到一个字符串以任意位置为中心的回文子串.其算法的基本原理就是利用已知回文串的左半部分来推导右半部分. 转:http://blog.sina.com.c ...

  5. BZOJ 2565: 最长双回文串 [Manacher]

    2565: 最长双回文串 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1842  Solved: 935[Submit][Status][Discu ...

  6. BZOJ.2565.[国家集训队]最长双回文串(Manacher/回文树)

    BZOJ 洛谷 求给定串的最长双回文串. \(n\leq10^5\). Manacher: 记\(R_i\)表示以\(i\)位置为结尾的最长回文串长度,\(L_i\)表示以\(i\)开头的最长回文串长 ...

  7. HYSBZ2565最长双回文串 Manacher

    顺序和逆序读起来完全一样的串叫做回文串.比如 acbca 是回文串,而 abc 不是( abc 的顺序为 “abc” ,逆序为 “cba” ,不相同). 输入长度为 n 的串 S ,求 S 的最长双回 ...

  8. 洛谷P4555 [国家集训队]最长双回文串(manacher 线段树)

    题意 题目链接 Sol 我的做法比较naive..首先manacher预处理出以每个位置为中心的回文串的长度.然后枚举一个中间位置,现在要考虑的就是能覆盖到i - 1的回文串中 中心最靠左的,和能覆盖 ...

  9. BZOJ3676 APIO2014 回文串 Manacher、SA

    传送门 首先一个结论:串\(S\)中本质不同的回文串个数最多有\(|S|\)个 证明考虑以点\(i\)结尾的所有回文串,假设为\(S[l_1,i],S[l_2,i],...,S[l_k,i]\),其中 ...

随机推荐

  1. 【leetcode】Shortest Palindrome(hard)★

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  2. IOS - 多态

    1. 多态性 多态性是个生物名词,用来表示生物体在生命周期中的不同形态,用在编程语言中则表示相同的方法名,但是却有不同的实现方式.或者说相同的名字,不同的类.我们来看一个书上的示例: #import  ...

  3. Jquery获取select 控件的change事件时选中的值

    HTML代码如下: <div class="col-sm-9 col-xs-12"> <select id="groupid" class=& ...

  4. 分页Bean终极封装

    package org.guangsoft.vo; import java.util.List; public class Page { private Integer pageNum; privat ...

  5. Python--set常用操作函数

    python提供了常用的数据结构,其中之一就是set,python中的set是不支持索引的.值不能重复.无需插入的容器. 简单记录下set常用的操作函数: 1.新建一个set: set("H ...

  6. spring AOP 的几种实现方式(能测试)

    我们经常会用到的有如下几种 1.基于代理的AOP 2.纯简单Java对象切面 3.@Aspect注解形式的 4.注入形式的Aspcet切面 一.需要的java文件 public class ChenL ...

  7. Android Programming: Pushing the Limits -- Chapter 5: Android User Interface Operations

    多屏幕 自定义View 多屏幕 @.Android 4.2 开始支持多屏幕. @.举例: public class SecondDisplayDemo extends Activity { priva ...

  8. EF – 问题集锦

    1.对一个或多个实体的验证失败.有关详细信息,请参见“EntityValidationErrors”属性 在EF5.0修改实体的时候,出现“对一个或多个实体的验证失败.有关详细信息,请参见“Entit ...

  9. Java集合源码学习(五)几种常用集合类的比较

    这篇笔记对几个常用的集合实现,从效率,线程安全和应用场景进行综合比较. >>ArrayList.LinkedList与Vector的对比 (1)相同和不同都实现了List接口,使用类似.V ...

  10. 设计模式学习之策略模式(Strategy,行为型模式)(13)

    转载地址:http://www.cnblogs.com/zhili/p/StragetyPattern.html 一.引言 本文要介绍的策略模式也就是对策略进行抽象,策略的意思就是方法,所以也就是对方 ...