FROM:  http://hi.baidu.com/chenwenwen0210/item/482c84396476f0e02f8ec230

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<map>
#include<algorithm>
#include<queue>
using namespace std;
typedef __int64 lld;
  
const int MAX=1100000*2;
char str[MAX];//原字符串
char sb[MAX];
int p[MAX];//表示以i为中心的回文半径,
/*p[i]-1刚好是原字符串以第i个为中心的回文串长度。
画画图就知道了,因为两端配匹的肯定是字符g
*/
/*
Mancher主算法。
功能:求出以i为中心的回文半径p[i];
参数:传入构造好的字符串长度
特殊说明:因为前面加了一个无效字符,所以下标从1开始。
  
*/
void Manacher(int n)
{
    int i;
    int mx=0;//记录前面回文串最长影响到的地方。不一定是最长回文串造成的。
    int id;//最长影响串的ID;
    p[0]=0;
    for(i=1;i<n;i++)
    {
        p[i]=1;//至少是1
        if(mx>i)//i受到影响即,id+p[id]-1>=i;
        {
            p[i]=p[2*id-i];//2*id-i是i关于id的对称点相当于是id-(i-id);
            if(mx-i<p[i])p[i]=mx-i;
            //由于对称点的回文半径可能超过mx-i,因为mx后面的还没有配过
            //所以要取小的。等等继续配
        }
  
        //向两端配匹
        while(str[i-p[i]]==str[i+p[i]])p[i]++;
  
        if(i+p[i]>mx)
        {
            mx=i+p[i];
            id=i;
        }
    }
}
  
/*
功能:构造字符串,在每一个字符前插入一个,g,一般用'#'
第一个字符前面再插入,first,一般用'$'
最后再插入g字符
总之不是在输入中出现的字符就行了。
比如abb,构造成$#a#b#b#
参数:<first,第一个字符>,<g,一般字符>
  
返回值:构造好的字符串长度
*/
int pre(char first='$',char g='#')
{
    int i,n=0;
    strcpy(sb,str);
    str[0]=first;
    n++;
    for(i=0;sb[i];i++)
    {
        str[n++]=g;
        str[n++]=sb[i];
    }
  
    str[n++]=g;
    str[n]=0;
    return n;
}
int main()
{
    int n;
    int i;
    int CS=1;
    while(scanf("%s",str)!=EOF)
    {
        if(strcmp(str,"END")==0)break;
  
        n=pre();
        Manacher(n);
        int ans=0;
  
        for(i=1;i<n;i++)
        {
            if(p[i]>ans)ans=p[i];
        }
  
        printf("%d\n",ans-1);
    }
    return 0;
}

Manacher回文串算法学习记录的更多相关文章

  1. 学习manacher(最长公共回文串算法)

    给定一个字符串求出其中最长个公共回文串. 举列子: abab   -->回文串长度为2 以前的算法诸如: 扩展kmp求法过于麻烦,看到有一篇博文(http://leetcode.com/2011 ...

  2. 【2018.07.28】(字符串/回文串)学习Manacher算法小记

    主要是应用在回文串啦,原理也理解了老半天,如果没有图片的话,我也看不太懂它的原理 学习的灵感来源来自于:https://segmentfault.com/a/1190000008484167 /* 最 ...

  3. Manacher 算法-----o(n)回文串算法

    回文的含义是:正着看和倒着看相同,如abba和yyxyy        Manacher算法基本要点:用一个非常巧妙的方式,将所有可能的奇数/偶数长度的回文子串都转换成了奇数长度:在每个字符的两边都插 ...

  4. UVA 12378 Ball Blasting Game 【Manacher回文串】

    Ball Blasting Game Morteza is playing a ball blasting game. In this game there is a chain of differe ...

  5. HDU3068 最长回文 MANACHER+回文串

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3068 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符 ...

  6. 2015 UESTC Training for Search Algorithm & String - M - Palindromic String【Manacher回文串】

    O(n)的复杂度求回文串:Manacher算法 定义一个回文值,字符串S是K重回文串,当且仅当S是回文串,且其长度为⌊N/2⌋的前缀和长度为⌊N/2⌋的后缀是K−1重回文串 现在给一个2*10^6长度 ...

  7. Best Reward---hdu3613(manacher 回文串)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3613 题意就是给你一个串s 然后求把s分成两部分之后的价值总和是多少,分开的串 如果是回文那么价值就是 ...

  8. hdu----(4513)吉哥系列故事——完美队形II(manacher(最长回文串算法))

    吉哥系列故事——完美队形II Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)To ...

  9. 【模板】Manacher 回文串

    推荐两个讲得很好的博客: http://blog.sina.com.cn/s/blog_70811e1a01014esn.html https://segmentfault.com/a/1190000 ...

随机推荐

  1. 《c程序设计语言》读书笔记-4.2-扩充atof函数

    #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> ...

  2. mysql慢查询工具

    GeorgeHao 安装过程: [root@localhost-centos6 ~]# wget percona.com/get/pt-query-digest [root@localhost-cen ...

  3. js判断对象是否为数组

    1.ECMAScript5中有一个现成的方法:Array.isArray(). var obj = {1:[1],2:[2]}, arr = [1], str = "1"; Arr ...

  4. 使用java mail的网易smtp协议 发送邮件

    package com.enation.newtest; import java.security.GeneralSecurityException; import java.util.Propert ...

  5. Windows下MySQL安装配置与使用

    1.下载. 下载地址: http://downloads.mysql.com/archives/get/file/mysql-5.7.11-winx64.zip. NavicatforMySQL:ht ...

  6. 百度之星初赛(A)——T6

    度度熊的01世界 Problem Description 度度熊是一个喜欢计算机的孩子,在计算机的世界中,所有事物实际上都只由0和1组成. 现在给你一个n*m的图像,你需要分辨他究竟是0,还是1,或者 ...

  7. 我对 aspnetpager和repeater以及查询条件的封装

    /// <summary>         /// 绑定所有图片列表         /// </summary>         /// <param name=&qu ...

  8. 用户空间缺页异常pte_handle_fault()分析--(上)【转】

    转自:http://blog.csdn.net/vanbreaker/article/details/7881206 版权声明:本文为博主原创文章,未经博主允许不得转载. 前面简单的分析了内核处理用户 ...

  9. django返回响应对象

    Django的视图必须要返回一个HttpResponse对象(或者其子类对象),不能像flask一样直接返回字符串. Django: return HttpResponse("Hello&q ...

  10. 【C语言】指针增减

    int *pa = NULL; ; printf("%x\n", pb); char *pca = NULL; ; printf("%x\n", pcb); s ...