链接:

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

题意:

Andy the smart computer science student was attending an algorithms class when the professor asked the students a simple question, "Can you propose an efficient algorithm to find the length of the largest palindrome in a string?"

A string is said to be a palindrome if it reads the same both forwards and backwards, for example "madam" is a palindrome while "acm" is not.

The students recognized that this is a classical problem but couldn't come up with a solution better than iterating over all substrings and checking whether they are palindrome or not, obviously this algorithm is not efficient at all, after a while Andy raised his hand and said "Okay, I've a better algorithm" and before he starts to explain his idea he stopped for a moment and then said "Well, I've an even better algorithm!".

If you think you know Andy's final solution then prove it! Given a string of at most 1000000 characters find and print the length of the largest palindrome inside this string.

思路:

马拉车模板题.

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <sstream>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 1e6+10; int hw[MAXN*2], val[30], Sum[MAXN*2];
char s[MAXN], ss[MAXN*2]; void Manacher(char *str)
{
int maxr = 0, mid;
int len = strlen(str);
for (int i = 1;i < len;i++)
{
if (i < maxr)
hw[i] = min(hw[mid*2-i], maxr-i);
else
hw[i] = 1;
while (str[i+hw[i]] == str[i-hw[i]])
hw[i]++;
if (hw[i]+i > maxr)
{
maxr = hw[i]+i;
mid = i;
}
}
} void Change(char *str, char *to)
{
to[0] = to[1] = '#';
int len = strlen(str);
for (int i = 0;i < len;i++)
{
to[i*2+2] = str[i];
to[i*2+3] = '#';
}
to[len*2+2] = 0;
} int main()
{
int cnt = 0;
while (~scanf("%s", s))
{
if (s[0] == 'E')
break;
Change(s, ss);
Manacher(ss);
int ans = 0;
int len = strlen(ss);
for (int i = 0;i < len;i++)
ans = max(ans, hw[i]);
printf("Case %d: %d\n", ++cnt, ans-1);
} return 0;
}

POJ-3974-Palindrome(马拉车)的更多相关文章

  1. POJ 3974 Palindrome | 马拉车模板

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

  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 求最长回文串长度. 终于会了传说中的马拉车,激动.推荐一个很棒的博客:https://www.61mon ...

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

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

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

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

  6. POJ 3974 Palindrome 字符串 Manacher算法

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

  7. poj 3974 Palindrome (manacher)

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

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

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

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

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

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

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

随机推荐

  1. BandingList 泛型集合数据绑定

    public IList<Student> IStudent = new List<Student>();  public BindingList<Student> ...

  2. tp5 post接到的json被转义 问题解决

    今天做项目的时候前端需要可以保存可变数据, 然后原样返回给前端 接口 $data =input('post.');//用户唯一标识$goods = $data['goods']; $shopcuxia ...

  3. Scala学习九——文件和正则表达式

    一.本章要点 Source.fromFile(...).getLines.toArray输出文件的所有行; Source.fromFile(...).mkString以字符串形式输出文件内容; 将字符 ...

  4. asp.net 12 AJAX

    Javascript:ajax Ajax:get <%@ Page Language="C#" AutoEventWireup="true" CodeBe ...

  5. 适配方案(四)适配的基础知识之单位、分辨率、viewport

    适配的基础知识 一.理解单位 px.pt.pc.sp.em.rem.dpr.dp.dip.ppi.dpi.ldpi.mdpi.hdpi.xhdpi.xxhdpi 如果你是ios开发,你需要了解的单位: ...

  6. centos安装配置mariadb

    CentOS7下使用yum安装MariaDB CentOS 6 或早期的版本中提供的是 MySQL 的服务器/客户端安装包,但 CentOS 7 已使用了 MariaDB 替代了默认的 MySQL.M ...

  7. 01 Go之环境搭建

    1.1 Linux搭建Go环境 1.ssh远程登录Linux服务器,可选用iTerm.xshell ssh root@ip 2.建议配置好阿里云yum源.epel源 安装git yum install ...

  8. CSS总结六:动画(一)ransition:过渡、animation:动画、贝塞尔曲线、step值的应用

    transition-property transition-duration transition-timing-function transition-delay animation-name a ...

  9. Entity Framework:三种开发模式实现数据访问

    原文地址 http://blog.csdn.net/syaguang2006/article/details/19606715 前言 Entity Framework支持Database First. ...

  10. SQLServer 主键插入

    设置此命令后可以往主键插入值 set IDENTITY_INSERT 表名 on set IDENTITY_INSERT 表名 off 注意: 此语句是一个整体操作 反例: 先单步执行:set IDE ...