This article introduces the Manacher algorithm for obtaining the longest palindrome substring length of a string, and the extended application of Manacher algorithm
First introduce a violent solution to obtain the length of the longest palindrome substring of a string
Time complexity: 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 algorithm
Time complexity: 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;
}
Given a string, you can only add characters at the end, and add the least characters at the end to make the entire string a palindrome as a whole
Time complexity: 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;
}
If there is any infringement, please contact to delete it. If there is an error, please correct me, thank you