manacher算法计算最长回文子串

来源:互联网 发布:js undefined null nan 编辑:程序博客网 时间:2024/06/12 19:58

1、首先说明什么是计算"最长回文子串":

比如字符串:ababcdedcba,该字符串内部有不止一个回文子串,包括aba、abcdedcba,那么最长的回文子串就是abcdedcba


2、常规的办法:
常规的办法就是遍历,每次都根据可能的两类情况获取最长回文,一个是这段回文是中点是两个相同字符,如abcdeedcba,中点是两个e;另一个是这段回文的中点就是一个字符,如abcdedcba,中点就是e。如下:

std::string func1 (const std::string str) {int max = 0;std::string longest = "";for (size_t i = 0; i < str.length(); i++) {
//假设中点为单字符,length首先置1,然后找到一次左右相同的,length就加2
int length = 1;std::string curlongest;
for (size_t j = 1; i - j >= 0 && i + j < str.length(); j++) {if (str[i - j] == str[i + j]) {length += 2;curlongest = str.substr(i - j, 1 + j * 2);} else {break;}}if (length > max) {max = length;longest = curlongest;}
//这里一定首先判断是否是中点双字符的情况,某些互相抄袭的文档的没有这个判断是不对的!
//length首先置2,然后找到一次左右相同的,length就加2if (i != str.length() - 1 && str[i] == str[i + 1]) {length = 2;for (size_t j = 1; i - j >= 0 && i + j + 1 < str.length(); j++) {if (str[i - j] == str[i + j + 1]) {length += 2;curlongest = str.substr(i - j, 2 + j * 2);} else {break;}}}if (length > max) {max = length;longest = curlongest;}}return longest;}

这种方法比较容易想到,但遇上字符串比较多的情况,这个方法会慢点


3、manacher:

首先,manacher方法用插入符号的方式,去除了所谓中点为单字符/双字符的问题

比如对于abccba,我把字符串变为1a1b1c1c1b1a1,计算的是新字符串的第一个字符a到倒数第二个字符a的回文子串长度,以前的双中点字符cc,转化成了计算新字符串的两个字符c之间的字符1的回文子串,即不再有双中点字符情况的存在了。


其次,manacher方法省掉了很多比较计算。

这个方法建立在一个定理的前提上,如对于字符串:ababa,一眼就能看出来最大回文就是ababa,中点为第三个字符a,此外还能看出对于前三个和后三个字符,均为aba,也是一个回文,中点字符分别为第2个b和第四个b;

那么如果字符串改为abcbefebcba,同样,最大回文就是字符串本身,此外还有两个回文即两个bcb

那么如果字符串再改完xyzabcbefebcbazyx,最大回文仍然是字符串自身,此外有两个回文还是两个bcb


在第一次计算字符c的回文时,会计算到bcb,回文子串长度为3,

然后计算到f,发现一个巨大的回文子串即整个字符串自身,

然后计算下一个c,此时c处于f的回文范围以内,那么定理来了:"在已知最大回文子串的范围内部,范围内部任意一个位置的字符的回文子串长度,不会超过它的对称点的回文子串长度"

试想下,对于abcbefebcba111111,最大回文子串是中点为f的整个字符串本身,第二个c字符在这个回文子串的范围内,那么如不考虑后面的11111,它的回文子串长度肯定不会超过它的对称点的回文子串长度的,如果能超过,那么f的回文子串不可能是整个字符串本身还要更长,但实际不可能更长。

这能省去很多计算,如下:

//在原字符串插入字符1,如abcba,变成1a1b1c1b1a1
std::string insert_symbol (const std::string str) {std::string newstr = "";for (size_t i = 0; i < str.length(); i++) {newstr += "1";newstr += str[i];}newstr += "1";return newstr;}std::string func2 (const std::string str) {
//生成新字符串newstrstd::string newstr = insert_symbol(str);
//mx指的是,点位置+回文子串半价长度,最大的一个点,所伸展到字符串的位置,比如44444abcdedcba55555,(点位置+回文半径)最大的一个点是e,
所伸展到字符串的位置是最后一个字符a的位置,即mx。id即这个点的位置。
int mx = 0, id = 0;

//数组p,用于记录每个点的回文子串的半径长度,如abadedaba,对于e,它的长度是4,对于第一个b,长度为1。
int *p = new int[newstr.length()];memset(p, 0, sizeof(int) * newstr.length());for (int i = 1; i < newstr.length() - 1; i++) {//first get the minest of p[i](http://blog.csdn.net/xingyeyongheng/article/details/9310555)//if i is in range of previous huiwen, could calc part of huiwen in range mx for i, because in range not out of mx, the huiwen part of i could not bigger than mx//but out of mx, huiwen part of i still need calc
//这个是核心。落在mx内的点初始化时的半径是不可能越过mx的。这个不太容易说的清晰,一定要仔细想,一定要理解if (mx > i) {p[i] = ((mx - i) < p[id * 2 - i])?(mx - i):p[id * 2 - i];} else {p[i] = 0;}
//计算越过mx后的部分是不是还存在更长的回文子串的可能
//then actual calc p[i], because maybe (i + p[i]) is longer that out of mxint cur = p[i];for (int j = 1; i - cur - j >= 0 && i + cur + j < newstr.length(); j++) {if (newstr[i + cur + j] == newstr[i - cur - j]) {p[i]++;} else {break;}}
//下面两个判断是独立的,不可以写在一个if里,否则可以试试(有些互相抄袭的文章是写在一个if里的。。。)
//首先判断伸展范围mx是否变的更大了if (mx < i + p[i]) {mx = i + p[i];}
//然后判断半径最长的id是不是该换人了if (p[id] < p[i]) {id = i;}}
//id是半径最长的点,即最大回文子串的中点,p[id]是它的半径,可以通过这个计算出最长回文子串是什么std::string longest = "";for (size_t i = id - p[id]; i <= id + p[id]; i++) {if (newstr[i] != '1') {longest += newstr[i];}}delete [] p;return longest;}
如下是测试程序:
int main (int argc, char *argv[]) {
//随便多试试std::string str = "abcbabcdedcbabcdefghgfedcba";
str = argv[1];std::cout << func1(str) << std::endl;std::cout << func2(str) << std::endl;return 0;}


描述的不太清楚,回来再补上清晰的描述

                                             
0 0
原创粉丝点击