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

题意:求一给定字符串最长回文子串的长度

思路:直接套模板manacher算法

code:

 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = ;
char Ma[*MAXN];
int Mp[*MAXN]; void Manacher(char s[], int len)
{
int l = ;
Ma[l++] = 'S';
Ma[l++] = '#';
for (int i = ; i < len; ++i)
{
Ma[l++] = s[i];
Ma[l++] = '#';
}
Ma[l] = ;
int mx = ;
int id = ;
for (int i = ; i < l; ++i)
{
Mp[i] = mx > i ? min(Mp[*id-i], mx-i) : ;
while (Ma[i+Mp[i]] == Ma[i-Mp[i]]) ++Mp[i];
if (i + Mp[i] > mx)
{
mx = i +Mp[i];
id = i;
}
}
} char s[MAXN]; int main()
{
int cnt = ;
while (scanf("%s", s) != EOF)
{
if (strcmp(s, "END") == ) break;
int len = strlen(s);
Manacher(s, len);
int ans = ;
len = * len + ;
for (int i = ; i < len; ++i) ans = max(ans, Mp[i] - );
printf("Case %d: %d\n",++cnt, ans);
}
return ;
}

POJ 3974 Palindrome(最长回文子串)的更多相关文章

  1. Palindrome - POJ 3974 (最长回文子串,Manacher模板)

    题意:就是求一个串的最长回文子串....输出长度. 直接上代码吧,没什么好分析的了.   代码如下: ================================================= ...

  2. URAL 1297 Palindrome 最长回文子串

    POJ上的,ZOJ上的OJ的最长回文子串数据量太大,用后缀数组的方法非常吃力,所以只能挑个数据量小点的试下,真要做可能还是得用manacher.贴一下代码 两个小错,一个是没弄懂string类的sub ...

  3. 【URAL 1297】Palindrome 最长回文子串

    模板题,,,模板打错查了1h+QAQ #include<cmath> #include<cstdio> #include<cstring> #include< ...

  4. Ural 1297 Palindrome 【最长回文子串】

    最长回文子串 相关资料: 1.暴力法 2.动态规划 3.中心扩展 4.Manacher法 http://blog.csdn.net/ywhorizen/article/details/6629268 ...

  5. Ural 1297 Palindrome(后缀数组+最长回文子串)

    https://vjudge.net/problem/URAL-1297 题意: 求最长回文子串. 思路: 先将整个字符串反过来写在原字符串后面,中间需要用特殊字符隔开,那么只需要某两个后缀的最长公共 ...

  6. 【hiho一下】第一周 最长回文子串

    题目1:最长回文子串 题目原文:http://hihocoder.com/contest/hiho1/problem/1 [题目解读] 题目与 POJ 3974 palindrome 基本同样.求解最 ...

  7. Manacher算法 O(n) 求最长回文子串

    转自:http://bbs.dlut.edu.cn/bbstcon.php?board=Competition&gid=23474 其实原文说得是比较清楚的,只是英文的,我这里写一份中文的吧. ...

  8. 最长回文子串——manacher

    最长回文子串--Manacher 算法 (原版的博主的代码都是用py写的,这里改成c++) c++ 算法 字符串处理 0. 问题定义 最长回文子串问题:给定一个字符串,求它的最长回文子串长度. 如果一 ...

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

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

随机推荐

  1. ubuntu下编译内核驱动。

    目的: 1. 驱动热身.网上有很多类似的文章可供参考. 2. 在操作系统中, 编写这个设备的驱动. 3. 为写qemu的watchdog驱动练手. 有朋友问make的 watchdog驱动 需要什么准 ...

  2. [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(三)

    我们已经设计了一个基于qemu的watchdog了.下一步工作就是创建一个含有我们的watchdog的虚拟计算机器了. 准备工作: 1. 使用virt-manager或者virsh创建一个虚拟机器. ...

  3. 关于android:screenOrientation="portrait" 横竖屏切换

    当在AndroidManifest.xml文件中定义了android:screenOrientation="portrait",就表示当我们切换横竖屏的时候,屏幕的内容始终以竖屏显 ...

  4. Oracle聚合求和和聚合求积(顺便解决BOM展开的问题)

    本文参考网址:http://www.itpub.net/thread-1020772-1-1.html 我们在日常的工作中,经常遇到了针对某一列的值,进行求和,求平均值,在一些特殊的业务场景下,我们需 ...

  5. wcf xml消息客户端cookie

    public override bool Login(string userName, string password) { using (HttpClient client = new HttpCl ...

  6. math。h中的log函数的应用

    以10为底的log函数: 形式为 double  log10(double  x) 以e为底的log函数(即 ln)double log (double x) 如何表达log 以a为底b的对数: 用换 ...

  7. mahout算法源码分析之Collaborative Filtering with ALS-WR 并行思路

    Mahout版本:0.7,hadoop版本:1.0.4,jdk:1.7.0_25 64bit. mahout算法源码分析之Collaborative Filtering with ALS-WR 这个算 ...

  8. 关于gradle /Users/xxxx/Documents/workspace/fontmanager/.gradle/2.2.1/taskArtifacts/cache.properties (No such file or directory)报错办法

    转自:http://www.cnblogs.com/raomengyang/p/4367620.html   Android Studio报错: What went wrong: java.io.Fi ...

  9. MySQL 表分区的几种方法和注意

    分区方法1:Hash分区 例子: create table thash(x int ,y int) partition by hash(x) partitions 4; 就这么一句话表就分好区了.下一 ...

  10. director.js教程

    directive.js 初始化和注册路由 director.js 的主要对象是Router对象,构造方法如下: var router = new Router(routes); //routes为路 ...