【算法】最长回文子串

来源:互联网 发布:淘宝上说的厂家直销 编辑:程序博客网 时间:2024/05/16 02:00

最长回文子串

from  https://github.com/julycoding/The-Art-Of-Programming-By-July/blob/master/ebook/zh/01.05.md

题目描述

给定一个字符串,求它的最长回文子串的长度。

方法一:暴力枚举

#include "stdafx.h"#include "stdio.h"#include "string"#define MIN(a,b)  (a)>(b)?(b):(a)//枚举中心位置法int LongestPalindrome(const char *s ,int n){<span style="white-space:pre"></span>if (s==NULL || n<1)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>return -1;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>int cmax = 0;<span style="white-space:pre"></span>int c = 0;<span style="white-space:pre"></span>int i,j;<span style="white-space:pre"></span>for (i = 0; i<n; i++)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>for (j = 0;(j<=i) && (j<n-i);j++)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>if (s[i+j]!=s[i-j])<span style="white-space:pre"></span>{<span style="white-space:pre"></span>break;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>c = 2*j+1;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>if (c>cmax)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>cmax = c;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>for (j = 0;j<=i && j<n-i-1;j++)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>if (s[i+j+1]!=s[i-j])<span style="white-space:pre"></span>{<span style="white-space:pre"></span>break;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>c = 2*(j+1);<span style="white-space:pre"></span>}<span style="white-space:pre"></span>if (c>cmax)<span style="white-space:pre"></span>{<span style="white-space:pre"></span>cmax = c;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}<span style="white-space:pre"></span>return cmax;}
int _tmain(int argc, _TCHAR* argv[]){   char* p = "12212321";   int result = LongestPalindrome(p,8);   int result2 = Manacher(p,8);return 0;}

方法二:Manacher算法

int Manacher(const char *s ,int n){int n2 = 2*n+2;char *s2 = (char*)malloc(n2 * sizeof(char));int i,j;s2[0] = '$';for (i = 1; i<n2 ;i=i+2){s2[i] = '#';s2[i+1] = s[(i-1)/2];}printf("change to : %s\n",s2);int p[1000],mx = 0,id = 0;memset(p,0,sizeof(p));for (i = 1;s2[i] != '\0';i++){p[i] = mx>i?MIN(p[2*id-i],mx - i):1;while(s2[i+p[i]] == s2[i-p[i]])p[i]++;if (i+p[i]>mx){mx = i+p[i];id = i;}}int m = 0;for (i = 1;i<n2;i++){if (p[i]>m){m = p[i];}}return m-1;}
原理部分:

首先通过在每个字符的两边都插入一个特殊的符号,将所有可能的奇数或偶数长度的回文子串都转换成了奇数长度。比如 abba 变成 #a#b#b#a#, aba变成 #a#b#a#。

此外,为了进一步减少编码的复杂度,可以在字符串的开始加入另一个特殊字符,这样就不用特殊处理越界问题,比如$#a#b#a#。

以字符串12212321为例,插入#和$这两个特殊符号,变成了 S[] = "$#1#2#2#1#2#3#2#1#",然后用一个数组 P[i] 来记录以字符S[i]为中心的最长回文子串向左或向右扩张的长度(包括S[i])。

比如S和P的对应关系:

  • S # 1 # 2 # 2 # 1 # 2 # 3 # 2 # 1 #
  • P 1 2 1 2 5 2 1 4 1 2 1 6 1 2 1 2 1

可以看出,P[i]-1正好是原字符串中最长回文串的总长度,为5。

接下来怎么计算P[i]呢?Manacher算法增加两个辅助变量id和mx,其中id表示最大回文子串中心的位置,mx则为id+P[id],也就是最大回文子串的边界。得到一个很重要的结论:

  • 如果mx > i,那么P[i] >= Min(P[2 * id - i], mx - i)

下面,令j = 2*id - i,也就是说j是i关于id的对称点。

当 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之后的部分是否对称,再具体匹配。

此外,对于 mx <= i 的情况,因为无法对 P[i]做更多的假设,只能让P[i] = 1,然后再去匹配。



0 0
原创粉丝点击