最长回文子串—动态规划和Manacher算法(0(n)时间复杂度
来源:互联网 发布:sublime text 3编译js 编辑:程序博客网 时间:2024/05/01 12:56
动态规划:
dp[i][j] = 1 i==j
dp[i][j] = dp[i+1][j-1] + 2 str[i] == str[j]
dp[i][j] = max(dp[i+1][j], dp[i][j-1]) str[i] != str[j],不相等,等于前面的的最大值
由于计算第i个位置,需要第i+1 个位置,因此倒着计算
Manacher
http://blog.csdn.net/avenger_tao/article/details/51056159
#include <iostream>#include <string>#include <vector>using namespace std;/** * Manacher O(n) * @param string str */int manacher(string &str) { int length = str.size() * 2 + 1; string cur_str(length, '#'); // 字符串中间 插入 特殊字符 for (int i = 0; i < length; ++i) { cur_str[i] = (i % 2 == 1 ? str[(i+1)/2 - 1] : '#'); } cout << cur_str << endl; vector<int> RL(length, 0); // 每一个位置的回文串 长度 (不包含特殊字符) int maxLength= 0; // 最大长度 int pos = 0; // 当前最大字串对称位置 int maxRight = 0; // 当前最大字串所能到达的最右边 for (int i = 0; i < length; ++i) { if (i >= maxRight) { RL[i] = 1; // 超出最右位置,与之前的对称串没有关系 } else { // 当前位置 在最大回文串里面(maxRight 左边) int j = 2 * pos - i; // i 关于pos对称的j (j 以及求过) RL[i] = min(RL[j], maxRight - i); /* 因为 maxRight - i 的最大值是 maxRight - pos * RL[j] 的 maxRight 如果超过pos,则肯定取 maxGight-i */ } while (i - RL[i] >= 0 && i + RL[i] < length && cur_str[i-RL[i]] == cur_str[i+RL[i]]) { RL[i]++; } // 对称位置i + 回文串长度 (回文串长度 在cur_str里面是对称半径长度) if (RL[i] - 1 + i > maxRight) { maxRight = RL[i] + i - 1; pos = i; } maxLength = max(maxLength, RL[i]); } return maxLength - 1;}/** * 动态规划, O(n^2) */int dpAlgorithm(string &str) { int length = str.size(); vector<vector<int> > dp(length, vector<int>(length, 0)); for (int i = length - 1; i >= 0; i--) { dp[i][i] = 1; for (int j = i + 1; j < length; ++j) { if (str[i] == str[j]) { dp[i][j] = dp[i+1][j-1] + 2; } else { dp[i][j] = max(dp[i+1][j], dp[i][j-1]); } } }// end for return dp[0][length-1];}int main(){ string str; while (cin >> str) { cout << dpAlgorithm(str) << endl; cout << manacher(str) << endl; } return 0;}
0 0
- 最长回文子串—动态规划和Manacher算法(0(n)时间复杂度
- 最长回文子串 Manacher算法 时间复杂度O(N)
- Manacher算法,最长回文串,时间复杂度O(n)
- 最长回文(Manacher算法 时间复杂度O(n))
- Manacher算法解决最长回文子串问题---O(n)时间复杂度
- 最长回文子串模板---Manacher算法。时间复杂度O(N)
- 回文子串【Manacher】算法时间复杂度O(n)
- Manacher 算法讲解 O(N)复杂度的 最长回文子串求解
- 最长回文子串(Manacher's algorithm算法,复杂度O(n))
- 最长回文串 O(n)复杂度(manacher算法)
- 最长回文字串 Manacher算法 时间复杂度O(N)
- 【最长回文子串】Manache算法,O(N)时间复杂度
- 最长回文子串算法-- Manacher算法--O(n)
- Manacher算法--O(n)最长回文子串算法
- hihocoder #1032 : 最长回文子串(动态规划+manacher算法)
- 求最长回文子串0(n)manacher算法
- 求字符串的最长回文子串(manacher算法O(n)时间)
- Manacher 算法详解:O(n) 复杂度求最长回文子串
- Maven实践(三)---编写主代码
- 设计模式之装饰者模式(Decorator)
- 1.5输入若干个整数,求其最大值、最小值和平均值
- 深入浅出Java入门 之 运算符(2)
- Cookie的传递流程
- 最长回文子串—动态规划和Manacher算法(0(n)时间复杂度
- 17.03.04 字符串
- 二叉树的编号
- 抽象工厂
- 要啥自行车,共享单车都来了
- iOS--音频播放、录音、视频播放、拍照、视频录制
- 不错的IT学习网站跟论坛
- Spark SQL 常用操作
- 腾讯2017暑期实习生编程题-有趣的数字