Manacher算法获取最长回文子串长度及其扩展应用

这一篇介绍一下获取字符串最长回文子串长度的Manacher算法,以及Manacher算法的扩展应用

先介绍一个获取字符串最长回文子串长度的暴力解法

时间复杂度:O(N^2)

/* 
  @Author: lwl2020
  @Date: 2020-5-25
  @Description: 获取字符串最长回文子串长度的暴力解法
*/#include <iostream>
#include <string>
using namespace std;/*
    暴力解法:
       1.对字符串进行"插帧"操作,方便对奇数和偶数长度的字符串同意操作
       2.对每一个字符向两边扩展,直到扩不动
​
     时间复杂度:O(N^2)
*/class CGetLongestPalindromicSubstringLength {
public:
  int getLongestPalindromicSubstringLength(string& str) {
    if (str.length() < 2) {
      return str.length();
    }
    string processStr = getProcessStr(str);
    int longest = 0;
    for (int i = 0; i < processStr.length(); i++) {
      int tmpLength = 1;
      for (int j = 1;
        i - j >= 0 && i + j <= processStr.length() - 1 && processStr.at(i - j) == processStr.at(i + j);
        j++) {
        tmpLength += 2;
      }
      longest = longest >= tmpLength ? longest : tmpLength;
    }
    return longest / 2;
  }
private:
  /* 1.对字符串进行"插帧"操作,方便对奇数和偶数长度的字符串同意操作 */
  string getProcessStr(string& str) {
    string resStr("#");
    for (int i = 0; i < str.length(); i++) {
      resStr.append(str.at(i) + string("#"));
    }
    return resStr;
  }
};int main() {
  string str("123321");
  int longest = CGetLongestPalindromicSubstringLength().getLongestPalindromicSubstringLength(str);
  cout << longest << endl;system("pause");
  return 0;
}
Manacher算法

时间复杂度:O(N)

/*
  @Author: lwl2020
  @Date: 2020-5-26
  @Description: Manacher算法:时间复杂度O(N)获取字符串的最长回文子串长度
*/#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;/*
  Manacher算法思路:
    1.对字符串进行"插帧操作",方便对奇数和偶数长度的字符串统一处理
    2.若当前已计算过回文半径的字符所到达的最右边界不包含此时待计算的字符,直接进行暴力扩展
    3.若当前已计算过回文半径的字符所到达的最右边界包含此时待计算的字符有三种情况:
        1.待计算的字符关于最早到达最右边界的字符对称位置的字符的回文半径包含在最右回文半径中,
        不再进行扩展
      2.待计算的字符关于最早到达最右边界的字符对称位置的字符的回文半径超出最右回文半径,也不
        再进行扩展
      3.待计算的字符关于最早到达最右边界的字符对称位置的字符的回文半径正好等于最右回文半径,
        从最右回文半径的位置继续进行扩展
*/class CManacher {
public:
  int manacher(string& str) {
    if (str.length() < 2) {
      return str.length();
    }
    /* 对原始字符串进行"插帧操作",方便对奇数和偶数长度的字符串进行统一处理 */
    string processStr = getProcessStr(str);
    /* 存放以每一个字符为对称点的回文串长度 */
    vector<int> rArr(processStr.length(), 0);
    /* 回文串到达的最右边界 */
    int rR = -1;
    /* 第一次到达最右边界的字符 */
    int center = -1;
    /* 当前最长回文半径 */
    int max = 0;
    for (int i = 0; i < processStr.length(); i++) {
      rArr.at(i) = rR > i ? min(rArr.at(center * 2 - i), rR - i) : 1;
      while (i - rArr.at(i) >= 0 && i + rArr.at(i) < processStr.length()) {
        if (processStr.at(i - rArr.at(i)) == processStr.at(i + rArr.at(i))) {
          rArr.at(i)++;
        }
        else {
          break;
        }
      }
      if (i + rArr.at(i) > rR) {
        rR = i + rArr.at(i);
        center = i;
      }
      max = max < rArr.at(i) ? rArr.at(i) : max;
    }
    return max - 1;
  }private:
  string getProcessStr(string& str) {
    string resStr("#");
    for (int i = 0; i < str.length(); i++) {
      resStr.append(str.at(i) + string("#"));
    }
    return resStr;
  }
};int main() {
  string str("123321");
  int res = CManacher().manacher(str);
  cout << res << endl;system("pause");
  return 0;
}
给定一个字符串,只能在末尾添加字符,在末尾添加最少的字符,使整个字符串整体为回文串

时间复杂度:O(N)

/*
  @Author: lwl2020
  @Date: 2020-5-26
  @Description: 在字符串末尾添加最少的字符,使字符串整体为回文串
*/

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

class CAddStrToEnd {
public:
	void addShortestStrToEnd(string& str) {
		if (str.empty()) {
			return;
		}
		string processStr = getProcessStr(str);
		vector<int> rArr(processStr.length(), 0);
		int rR = -1;
		int center = -1;
		for (int i = 0; i < processStr.length(); i++) {
			rArr.at(i) = rR > i ? min(rArr.at(center * 2 - i), rR - i) : 1;
			while (i - rArr.at(i) >= 0 && i + rArr.at(i) < processStr.length()) {
				if (processStr.at(i - rArr.at(i)) == processStr.at(i + rArr.at(i))) {
					rArr.at(i)++;
				}
				else {
					break;
				}
			}
			if (i + rArr.at(i) > rR) {
				rR = i + rArr.at(i);
				center = i;
			}
			/* 当最长回文右边界到达字符串末尾时,
			   将未包含在此时的最长回文串中的字符串逆序添加到字符串最后 */
			if (rR == processStr.length()) {
				string subStr = str.substr(0, str.length() - rArr.at(center) + 1);
				reverse(subStr.begin(), subStr.end());
				str += subStr;
				break;
			}
		}
	}

private:
	string getProcessStr(string& str) {
		string resStr("#");
		for (int i = 0; i < str.length(); i++) {
			resStr.append(str.at(i) + string("#"));
		}
		return resStr;
	}
};

int main() {
	string str("123214");
	CAddStrToEnd().addShortestStrToEnd(str);
	cout << str << endl;

	system("pause");
	return 0;
}

如有侵权,请联系删除,如有错误,欢迎大家指正,谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值