题链:

http://poj.org/problem?id=3974

题解:

Manacher

求最长回文串长度。

终于会了传说中的马拉车,激动。
推荐一个很棒的博客:https://www.61mon.com/index.php/archives/181/

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#define MAXN 2005000
#define filein(x) freopen(#x".in","r",stdin);
#define fileout(x) freopen(#x".out","w",stdout);
using namespace std;
char T[MAXN]; int cnt;
int trans(char *S){
int lS=strlen(S),lT=2;
T[0]='&'; T[1]='#';
for(int i=0;i<lS;i++){
T[lT++]=S[i];
T[lT++]='#';
}
return lT;
}
int Manacher(int N,char *S){
static int p[MAXN],ans,pos,maxr;
pos=maxr=ans=0;
for(int i=1;i<N;i++){
if(i<maxr) p[i]=min(p[2*pos-i],maxr-i);
else p[i]=1;
while(S[i+p[i]]==S[i-p[i]]) {
if(i+p[i]>maxr){
maxr=i+p[i],pos=i;
if(S[i+p[i]-1]!='#')cnt++;
}
p[i]++;
}
ans=max(ans,p[i]-1);
}
return ans;
}
int main()
{
static char S[MAXN],cas=0;
while(1){
scanf("%s",S);
if(S[0]=='E'&&S[1]=='N'&&S[2]=='D'&&S[3]==0) break;
int N=trans(S); cnt=0;
int ans=Manacher(N,T);
printf("Case %d: %d\n",++cas,ans);
}
return 0;
}

●POJ 3974 Palindrome(Manacher)的更多相关文章

  1. poj 3974 Palindrome (manacher)

    Palindrome Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 12616   Accepted: 4769 Desc ...

  2. POJ 3974 Palindrome

    D - Palindrome Time Limit:15000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  3. POJ 3974 Palindrome 字符串 Manacher算法

    http://poj.org/problem?id=3974 模板题,Manacher算法主要利用了已匹配回文串的对称性,对前面已匹配的回文串进行利用,使时间复杂度从O(n^2)变为O(n). htt ...

  4. POJ 3974 Palindrome(最长回文子串)

    题目链接:http://poj.org/problem?id=3974 题意:求一给定字符串最长回文子串的长度 思路:直接套模板manacher算法 code: #include <cstdio ...

  5. POJ 3974 - Palindrome - [字符串hash+二分]

    题目链接:http://poj.org/problem?id=3974 Time Limit: 15000MS Memory Limit: 65536K Description Andy the sm ...

  6. POJ 3974 Palindrome | 马拉车模板

    给一个字符串,求最长回文字串有多长 #include<cstdio> #include<algorithm> #include<cstring> #define N ...

  7. 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome

    题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...

  8. POJ 3974 Palindrome (算竞进阶习题)

    hash + 二分答案 数据范围肯定不能暴力,所以考虑哈希. 把前缀和后缀都哈希过之后,扫描一边字符串,对每个字符串二分枚举回文串长度,注意要分奇数和偶数 #include <iostream& ...

  9. POJ 1159 Palindrome(字符串变回文:LCS)

    POJ 1159 Palindrome(字符串变回文:LCS) id=1159">http://poj.org/problem? id=1159 题意: 给你一个字符串, 问你做少须要 ...

随机推荐

  1. Beta敏捷冲刺每日报告——Day4

    1.情况简述 Beta阶段Scrum Meeting 敏捷开发起止时间 2017.11.5 00:00 -- 2017.116 00:00 讨论时间地点 2017.11.5 晚9:30,电话会议会议 ...

  2. jQuery函数学习

    函数:after(content) 功能:在每个匹配的元素后面添加html内容 返回:jQuery对象 参数:content (<Content>): Content to insert ...

  3. 去掉xcode编译warning:ld: warning: directory not found for option '-L

    选择工程, 编译的 (targets) 选择 Build Settings 菜单 查找 Library Search Paths 和 Framework Search Paths, 删掉编译报warn ...

  4. aws中的路由表

    参考官方文档: 由表中包含一系列被称为路由的规则,可用于判断网络流量的导向目的地. 在您的 VPC 中的每个子网必须与一个路由表关联:路由表控制子网的路由.一个子网一次只能与一个路由表关联,但您可以将 ...

  5. 亚马逊AWS学习——VPC里面几个概念的关系

    VPC中涉及几个概念: VPC 子网 路由表 Internet网关 安全组 今天来讲讲这几个概念之间的关系. 1. VPC 说的就是VPC,当然VPC范围是最大的,VPC即virtual privat ...

  6. awk sed tr替换换行符为逗号,并合并为一行

    在群里看到的.记录以备用.  sed 帮助命令:http://man.linuxde.net/sed 文件里有如下行,我想将每行的回车符替换为逗号,并将所有行合并到一行,用awk或sed怎么写啊TOP ...

  7. api-gateway实践(15)3.6JL分支和3.7并行改造需求

    一.名称改为"API网关" --哪个地方的名称?二.开发者视图中,API网关显示两个视图. 1. 服务分类视图:支持按照业务分为多个类别,分类方式参照应用服务化的分类:人像比对.自 ...

  8. 新概念英语(1-93)Our new neighbour

    Lesson 93 Our new neighbour 我们的新邻居 Listen to the tape then answer this question. Why is Nigel a luck ...

  9. linux下的Shell编程(8)自定义函数

    Shell Script中也可以使用自定义的函数,其语法形式如下: functionname() { - }

  10. 新概念英语(1-65)Not a Baby

    新概念英语(1-65)Not a Baby Does Jill take the key to the front door? A:What are you going to do this even ...