题目链接:http://hihocoder.com/problemset/problem/1032

#include <bits/stdc++.h>
using namespace std; bool table[][] = {false}; string longestPalindromeDP(string s)
{
int n = s.length();
int longestBegin = ;
int maxLen = ;
memset(table,,sizeof(table));
for (int i = ; i < n; i++)
table[i][i] = true; //前期的初始化 for (int len = ; len <= n; len++)
{
for (int i = ; i < n-len+; i++)
{
int j = i+len-;
if (s[i] == s[j] && table[i+][j-])
{
table[i][j] = true;
longestBegin = i;
maxLen = len;
}
}
}
return s.substr(longestBegin, maxLen);
} int main()
{
int t;
cin>>t;
while(t--)
{
string str;
cin>>str;
string ans = longestPalindromeDP(str);
cout<<ans.length()<<endl;
}
return ;
}
#include <bits/stdc++.h>
using namespace std; const int maxn = ; char instr[maxn],str[maxn*];
int rad[maxn*]; int Manacher()
{
int i,j,maxx;
int n = strlen(instr);
memset(str,'#',sizeof(str));
for(i=;i<n;i++)
str[(i+)<<] = instr[i]; n = (n+)<<;
str[n] = '$';
int maxRad;
maxRad = j = maxx = ;
for(i = ;i<n;i++)
{
if(i<maxx)
rad[i] = min(rad[*j-i],maxx-i);
else rad[i] = ; while(str[i-rad[i]]==str[i+rad[i]])
rad[i] ++;
if(maxRad<rad[i])
maxRad = rad[i];
if(rad[i]+i>maxx)
{
j = i;
maxx = rad[i] + i;
} }
return maxRad; } int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s",instr);
printf("%d\n",Manacher()-);
}
return ;
}

先是用DP写了一下,

DP方程,就是用一个二维数组标记table[i][j] 字符串i,到j是否构成回文串,然后枚举最大长度len,

要是两端相等,并且,可以扩展,那么longestBegin = i,maxlen = len;时间复杂度还是O(n^2),并且数组都开不了。

然后就是Manacher算法:

参考:http://www.cnblogs.com/lv-2012/archive/2012/11/15/2772268.html

先扩充为两倍的字符串,rad[i]表示新的字符串第I个位置可以向左向右匹配的最大距离。求出这个rad数组,有一个结论,rad - 1就是原串对应的位置能匹配的最大长度。

那么怎么求rad数组:

求rad[i] 的时候,如果知道rad 前面的值,还有前面有个位置 ID,能够扩充的最大距离是max,

那么rad = min(rad[2*id-i],max-i);

原因:

当 mx - i > P[j] 的时候,以S[j]为中心的回文子串包含在以S[id]为中心的回文子串中,由于 i 和 j 对称,
以S[i]为中心的回文子串必然包含在以S[id]为中心的回文子串中,所以必有 P[i] = P[j],见下图。
   
  
   当 P[j] > mx - i 的时候,以S[j]为中心的回文子串不完全包含于以S[id]为中心的回文子串中,但是基于
对称性可知,下图中两个绿框所包围的部分是相同的,也就是说以S[i]为中心的回文子串,其向右至少会
扩张到mx的位置,也就是说 P[i] >= mx - i。至于mx之后的部分是否对称,就只能老老实实去匹配了。
  

hiho 第1周 最长回文子串的更多相关文章

  1. hiho一下 第一周 最长回文子串

    时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这 ...

  2. hiho一下第一周 最长回文子串

    题目链接:http://hihocoder.com/contest/hiho1/problem/1 #include <iostream> #include <cstdio> ...

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

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

  4. hihoCoder第一周---最长回文子串(1032)

    其实这就是mancher算法的板子题,贴个代码好了. 思想请见我的另一篇博客: https://blog.csdn.net/qq_41090676/article/details/86768361 # ...

  5. hihoCoder hiho一下 第一周 #1032 : 最长回文子串 (Manacher)

    题意:给一个字符串,求最长回文子串的长度. 思路: (1)暴力穷举.O(n^3) -----绝对不行. 穷举所有可能的出现子串O(n^2),再判断是否回文O(n).就是O(n*n*n)了. (2)记录 ...

  6. hiho #1032: 最长回文子串

    #1032 : 最长回文子串 时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在 ...

  7. HiHo 1032 最长回文子串 (Manacher算法求解)

    /** * 求解最长回文字串,Manacher算法o(n)求解最长回文子串问题 **/ #include<cstdio> #include<cstdlib> #include& ...

  8. 最长回文子串-LeetCode 5 Longest Palindromic Substring

    题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  9. 最长回文子串(Longest Palindromic Substring)

    这算是一道经典的题目了,最长回文子串问题是在一个字符串中求得满足回文子串条件的最长的那一个.常见的解题方法有三种: (1)暴力枚举法,以每个元素为中心同时向左和向右出发,复杂度O(n^2): (2)动 ...

随机推荐

  1. Linux Ubuntu常用终端命令

    查看cpu温度: 安装命令如下:sudo apt-get install acpi 然后acpi -t 即可 输入法配置窗口命令: fcitx-config-gtk3 im-config 任务管理器命 ...

  2. C# 问题解决思路--《数组bytes未定义》,ASP.NET页面加载顺序

    好久没写博客了,废话不多说,直接说问题. 问题发生情况,首先这个是老项目,然后我是第一次修改.当我解决了各种引用,数据库配置之后等类似的问题,我启动的项目的时候,无任何问题,但是当我点击页面的按钮的时 ...

  3. bzoj3304 [Shoi2005]带限制的最长公共子序列

    dp,时间复杂度O(n^3),f[i][j][k]表示a串到i,b串到j的时候,匹配了c串的k位,要用滚动数组 代码 #include<cstring> #include<algor ...

  4. java冒泡排序算法

    /** * 冒泡排序算法:每次 * @author shaobn * @description 每次都会确定一个最大的值 即固定了,所以会每次-i; */ public class Test1 { p ...

  5. 10---Net基础加强

    复习: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...

  6. clock

    Prime Time中的clock分析包括: 1)Multiple clocks,clock from port/pin,virtual clock. 2)Clock network delay an ...

  7. async fifo

    异步fifo,解决跨时钟域的数据传输问题. 由binary,gray两种counter组成,在读写domain之间,只传输gray code. 主要的设计难点在empty和full的产生中. empt ...

  8. 二项分布 多项分布 伽马函数 Beta分布

    http://blog.csdn.net/shuimu12345678/article/details/30773929 0-1分布: 在一次试验中,要么为0要么为1的分布,叫0-1分布. 二项分布: ...

  9. C# 文件读取(一)

    1. 读写文件的步骤: 创建一个文件流 -- 创建相应的读写器 -- 执行读写操作 -- 关闭读写器 -- 关闭文件流 创建一个文件流:   FileStream objfs = new FileSt ...

  10. nginx在windwos中的使用

    本文章参考了 nginx for windows的介绍:http://nginx.org/en/docs/windows.html 你从官网上下载到的是一个 zip 格式的压缩包,首先要把压缩包解压. ...