HDU 3068 Manacher裸题

 #include <cstdio>
#include <cstring>
const int Maxn=;
char Str[Maxn<<],STR[Maxn<<];
int Len,x,P[Maxn<<],Id,Mx;
inline int Max(int x,int y) {return x>y?x:y;}
inline int Min(int x,int y) {return x>y?y:x;}
inline Init()
{
Len=strlen(Str+);
for (int i=;i<=Len;i++) STR[i]=Str[i];
Str[]='$'; Str[]='#';
for (int i=;i<=Len;i++) Str[i<<]=STR[i],Str[i<<|]='#';
Len=Len<<|; Id=;Mx=;
}
inline int Manacher()
{
for (int i=;i<=Len;i++)
{
if (Mx>i) P[i]=Min(P[*Id-i],Mx-i); else P[i]=;
while (Str[i+P[i]]==Str[i-P[i]]) P[i]++;
if (i+P[i]>Mx) Mx=P[i]+i,Id=i;
}
int Ret=;
for (int i=;i<=Len;i++) Ret=Max(Ret,P[i]);
return Ret-;
}
int main()
{
while (scanf("%s",Str+)!=EOF)
{
Init();
printf("%d\n",Manacher());
}
return ;
}

C++

BZOJ 3790 线段求交

用Manacher把最大的回文半径求出然后线段求交即可

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#define mp make_pair
#define pb push_back
#define PA pair<int,int>
#define fi first
#define se second
using namespace std;
const int Maxn=;
const int Inf=0x3f3f3f3f;
char S[Maxn],Str[Maxn];
int P[Maxn],c[Maxn];
int Len;
vector<PA> Line;
inline int Min(int x,int y) {return x>y?y:x;}
inline int lowbit(int x) {return x&-x;}
inline int Query(int x)
{
if (x==) return ;
int Ret=Inf;
for (int i=x;i<=Len;i+=lowbit(i)) Ret=Min(Ret,c[i]);
return Ret;
}
inline void Update(int x,int y)
{
for (int i=x;i;i-=lowbit(i)) c[i]=Min(c[i],y);
}
inline void Manacher()
{
int m=Len<<|;
for (int i=;i<=Len;i++)
{
S[i<<]=Str[i];
S[i<<|]='#';
}
S[]='#'; S[]='&'; S[m+]='^';
int Mx=,Id=;
for (int i=;i<=m;i++)
{
if (i<Mx) P[i]=Min(Mx-i,P[*Id-i]); else P[i]=;
while (S[i-P[i]]==S[i+P[i]]) P[i]++;
if (i+P[i]>Mx) Mx=i+P[i],Id=i;
int l=(i-P[i])/+,r=(i+P[i])/-;
if (l<=r) Line.pb(mp(r,l));
}
}
int main()
{
while (scanf("%s",Str+)!=EOF)
{
Len=strlen(Str+); Line.clear();
Manacher();
sort(Line.begin(),Line.end());
for (int i=;i<=Len;i++) c[i]=Inf;
int Ans=Inf;
for (int i=;i<Line.size();i++)
{
int x=Query(Line[i].se-)+;
Update(Line[i].fi,x);
if (Line[i].fi==Len) Ans=Min(Ans,x);
}
printf("%d\n",Ans-);
}
return ;
}

C++

Manacher的更多相关文章

  1. HDU3068 回文串 Manacher算法

    好久没有刷题了,虽然参加过ACM,但是始终没有融会贯通,没有学个彻底.我干啥都是半吊子,一瓶子不满半瓶子晃荡. 就连简单的Manacher算法我也没有刷过,常常为岁月蹉跎而感到后悔. 问题描述 给定一 ...

  2. manacher算法专题

    一.模板 算法解析:http://www.felix021.com/blog/read.php?2040 *主要用来解决一个字符串中最长回文串的长度,在O(n)时间内,线性复杂度下,求出以每个字符串为 ...

  3. BZOJ2342 Manacher + set

    题一:别人介绍的一道题,题意是给出一个序列,我们要求出一段最常的连续子序列,满足:该子序列能够被平分为三段,第一段和第二段形成回文串,第二段和第三段形成回文串. 题二:BZOJ2342和这题非常的相似 ...

  4. lintcode最长回文子串(Manacher算法)

    题目来自lintcode, 链接:http://www.lintcode.com/zh-cn/problem/longest-palindromic-substring/ 最长回文子串 给出一个字符串 ...

  5. Manacher's algorithm

    Manacher's algorithm 以\(O(n)\)的线性时间求一个字符串的最大回文子串. 1. 预处理 一个最棘手的问题是需要考虑最长回文子串的长度为奇数和偶数的情况.我们通过在任意两个字符 ...

  6. 1089 最长回文子串 V2(Manacher算法)

    1089 最长回文子串 V2(Manacher算法) 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 回文串是指aba.abba.cccbccc.aaaa ...

  7. Manacher's Algorithm 马拉车算法

    这个马拉车算法Manacher‘s Algorithm是用来查找一个字符串的最长回文子串的线性方法,由一个叫Manacher的人在1975年发明的,这个方法的最大贡献是在于将时间复杂度提升到了线性,这 ...

  8. 51nod1089(最长回文子串之manacher算法)

    题目链接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1089 题意:中文题诶~ 思路: 我前面做的那道回文子串的题 ...

  9. HDU - 3948 后缀数组+Manacher

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3948 题意:给定一个字符串,求字符串本质不同的回文子串个数. 思路:主要参考该篇解题报告 先按照man ...

  10. LeetCode 5 Longest Palindromic Substring manacher算法,最长回文子序列,string.substr(start,len) 难度:2

    https://leetcode.com/problems/longest-palindromic-substring/ manacher算法相关:http://blog.csdn.net/ywhor ...

随机推荐

  1. Unity在编辑器状态下清空控制台信息

    public static void ClearConsole() { var assembly = System.Reflection.Assembly.GetAssembly(typeof(Uni ...

  2. iframe显示错误页面

    当系统出现异常时,ifrme中显示的内容为错也页面,而不是罪顶层的框架显示错误内容,此时的解决办法是在错误页面或相关的登录页面中加入 错误页面加载的JS如下 <script type=" ...

  3. ipad开发:二维码扫描,摄像头旋转角度问题解决办法

    之前一直是在手机上开发,用系统原生二维码扫描功能,一点问题都没有,但是在ipad上,用户是横屏操作的,虽然界面旋转了,是横屏的,但是摄像头里显示的依然是竖屏效果,也就是说从摄像头里看到的和人眼看到的内 ...

  4. “stdafx.h”: No such file or directory

    “stdafx.h”: No such file or directory 一般原因是建工程的时候选择了空工程,然后添加现有源文件(含stdafx.cpp) 或者 修改了已有的stdafx.cpp 或 ...

  5. C++全局和静态变量初始化

    转自:http://www.cnblogs.com/zhenjing/archive/2010/10/15/1852116.html 对于C语言的全局和静态变量,不管是否被初始化,其内存空间都是全局的 ...

  6. HDU3333 Turing Tree 树状数组+离线处理

    Turing Tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. GZFramwork数据库层《二》单据表增删改查(自动生成单据号码)

    运行效果: 使用代码生成器(GZCodeGenerate)生成tb_EmpLeave的Model 生成器源代码下载地址: https://github.com/GarsonZhang/GZCodeGe ...

  8. O(1) Check Power of 2 - LintCode

    examination questions Using O(1) time to check whether an integer n is a power of 2. Example For n=4 ...

  9. 《BI那点儿事》数据流转换——字词查找转换

    字词查找转换将从转换输入列的文本中提取的字词与引用表中的字词进行匹配,然后计算出查找表中的字词在输入数据集中出现的次数,并将计数与引用表中的此字词一并写入转换输出的列中.此转换对于创建基于输入文本并带 ...

  10. 《BI项目笔记》历年理化指标分析Cube的建立

    该系统属于数据仓库系统,与传统的管理信息系统有本质差别,是“面向主题”设计的.“面向主题”的方式,既有利于数据组织和利用,又有利于用户的理解和使用. 分析主题主要维度:烟叶级别.烟叶级别按等级信息.烟 ...