POJ 3974 Palindrome | 马拉车模板
给一个字符串,求最长回文字串有多长
#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 1000005
using namespace std;
int n,m,T,p[N*2],ans;
char s[2*N],t[N];
void manacher()
{
int id=0,pos=0,x=0;
for (int i=1;i<+n;i++)
{
if (pos>i) x=min(p[id*2-i],pos-i);
else x=1;
while (s[i-x]==s[i+x]) x++;
if (i+x>pos) pos=i+x,id=i;
p[i]=x;
}
}
int main()
{
while (scanf("%s",t+1) && t[1]!='E')
{
ans=0;
m=strlen(t+1);
s[n=0]='!';
for (int i=1;i<=m;i++)
{
s[++n]='#';
s[++n]=t[i];
}
s[++n]='#';
s[n+1]='?';
manacher();
for (int i=1;i<=n;i++)
ans=max(ans,p[i]);
printf("Case %d: %d\n",++T,ans-1);
}
return 0;
}
POJ 3974 Palindrome | 马拉车模板的更多相关文章
- POJ 3974 Palindrome
D - Palindrome Time Limit:15000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- POJ 3974 Palindrome(最长回文子串)
题目链接:http://poj.org/problem?id=3974 题意:求一给定字符串最长回文子串的长度 思路:直接套模板manacher算法 code: #include <cstdio ...
- ●POJ 3974 Palindrome(Manacher)
题链: http://poj.org/problem?id=3974 题解: Manacher 求最长回文串长度. 终于会了传说中的马拉车,激动.推荐一个很棒的博客:https://www.61mon ...
- POJ 3974 - Palindrome - [字符串hash+二分]
题目链接:http://poj.org/problem?id=3974 Time Limit: 15000MS Memory Limit: 65536K Description Andy the sm ...
- POJ 3974 Palindrome 字符串 Manacher算法
http://poj.org/problem?id=3974 模板题,Manacher算法主要利用了已匹配回文串的对称性,对前面已匹配的回文串进行利用,使时间复杂度从O(n^2)变为O(n). htt ...
- poj 3974 Palindrome (manacher)
Palindrome Time Limit: 15000MS Memory Limit: 65536K Total Submissions: 12616 Accepted: 4769 Desc ...
- 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome
题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...
- POJ 3974 Palindrome (算竞进阶习题)
hash + 二分答案 数据范围肯定不能暴力,所以考虑哈希. 把前缀和后缀都哈希过之后,扫描一边字符串,对每个字符串二分枚举回文串长度,注意要分奇数和偶数 #include <iostream& ...
- hdu 3068 最长回文子串 马拉车模板
前几天用后缀数组写过一次这题,毫无疑问很感人的TLE了-_-|| 今天偶然发现了马拉车模板,O(N)时间就搞定 reference:http://acm.uestc.edu.cn/bbs/read.p ...
随机推荐
- Xtrabackup实现MySQL备份
一.xtrabackup介绍 Xtrabackup是一个对InnoDB做数据备份的工具,支持在线热备份(备份时不影响数据读写)它由percona提供的mysql数据库备份工具,据官方介绍,这也是世界上 ...
- 【Ecshop】商品数据采集扩展
一个自用的Ecshop商品数据采集程序 ->到此下载
- 【LeetCode #179】Largest Number 解题报告
原题链接:Largest Number 题目描述: Given a list of non negative integers, arrange them such that they form th ...
- Linux入门篇(五)——Shell(一)
这一系列的Linux入门都是本人在<鸟哥的Linux私房菜>的基础上总结的基本内容,主要是记录下自己的学习过程,也方便大家简要的了解 Linux Distribution是Ubuntu而不 ...
- iOS各个版本的特点
一.导航控制器中: iOS7: 栈顶控制器默认是320*480:控制器有64的高度看不见:
- poj1182食物链
Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到 ...
- js调用js的方法
a.js文件调用b.js文件时,需要在a.js的第一行添加代码 document.write(" <script language=\"javascript\" s ...
- 修改 cmd 字体为 Consolas
windows 下的 cmd 窗口默认的字体有点难看,长时间使用操作 node.js 有点小疲劳,可以修改注册表替换字体为 Consolas,并且可以全屏 cmd 窗口,代码如下: Windows R ...
- IAR FOR STM8S 错误 An error occurred while retrieving GDI features: gdi-error [40201]解决方法
今早使用IAR调试编译调试一个工程,发现IAR竟然出现如下错误信息 An error occurred ]: Can't access configuration database 在网上查看了一下, ...
- 12,DBUtils - Python数据库连接池
创建数据库连接池: import time import pymysql import threading from DBUtils.PooledDB import PooledDB, SharedD ...