5.Longest Palindromic Substring (String; DP, KMP)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
思路I:动态规划,遍历到i的时候要保证之前的元素都已经计算过状态,所以遍历顺序同插入排序,时间复杂度O(n2)
class Solution {
public:
string longestPalindrome(string s) {
int len = s.length();
if(len==) return s;
bool dp[len][len]={false};
int maxLen=;
int start=;
//initialize
for(int i = ; i < len; i++){
dp[i][i]=true;
}
for(int i=; i<len; i++){
for(int j = ; j<i; j++){
if(s[i]==s[j] && (j==i- || dp[j+][i-])){
dp[j][i]=true;
if(i-j+ > maxLen){
maxLen=i-j+;
start=j;
}
}
}
}
return s.substr(start, maxLen);
}
};
思路II:KMP,一种字符串匹配方法。

- If P[ i ] ≤ R – i, we set P[ i ] to P[ i' ] which takes exactly one step.
- Otherwise we attempt to change the palindrome’s center to i by expanding it starting at the right edge, R. Extending R (the inner while loop) takes at most a total of N steps, and positioning and testing each centers take a total of N steps too. Therefore, this algorithm guarantees to finish in at most 2*N steps, giving a linear time solution.
那么总共时间复杂度最坏是O(n2),最好是O(n)
class Solution {
public:
string preProcess(string s) {
int n = s.length();
if (n == ) return "^$";
string ret = "^"; //开始符^
for (int i = ; i < n; i++)
ret += "#" + s.substr(i, );
ret += "#$"; //结束符$
return ret;
}
string longestPalindrome(string s) {
string T = preProcess(s);
int n = T.length();
int *P = new int[n]; //状态数组长度等于原来字符串的长度,不用给#计算状态
int C = , R = ;
for (int i = ; i < n-; i++) {
int i_mirror = *C-i; // equals to i_mirror = C - (i-C)
//if p[i_mirror] < R-i: set p[i] to p[i_mirror]
P[i] = (R > i) ? min(R-i, P[i_mirror]) : ;
//else: Attempt to expand palindrome centered at i
while (T[i + + P[i]] == T[i - - P[i]]) //因为有哨兵^$所以不用担心越界; +1, -1检查下一个元素是否相等,若相等,扩大p[i]
P[i]++;
//if the palindrome centered at i does expand past R
if (i + P[i] > R) {
C = i;
R = i + P[i];
}
}
// Find the maximum element in P.
int maxLen = ;
int centerIndex = ;
for (int i = ; i < n-; i++) {
if (P[i] > maxLen) {
maxLen = P[i];
centerIndex = i;
}
}
delete[] P;
return s.substr((centerIndex - - maxLen)/, maxLen);
}
};
5.Longest Palindromic Substring (String; DP, KMP)的更多相关文章
- 5. Longest Palindromic Substring (DP)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- Leetcode:【DP】Longest Palindromic Substring 解题报告
Longest Palindromic Substring -- HARD 级别 Question SolutionGiven a string S, find the longest palindr ...
- 5. Longest Palindromic Substring(最长回文子串 manacher 算法/ DP动态规划)
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- *5. Longest Palindromic Substring (dp) previous blogs are helpful
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- LeetCode第[5]题(Java):Longest Palindromic Substring 标签:String、动态规划
题目中文:求最长回文子串 题目难度:Medium 题目内容: Given a string s, find the longest palindromic substring in s. You ma ...
- 【LeetCode】Longest Palindromic Substring 解题报告
DP.KMP什么的都太高大上了.自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. Yo ...
- [LeetCode] Longest Palindromic Substring 最长回文串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- Leetcode Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 【leedcode】 Longest Palindromic Substring
Given a , and there exists one unique longest palindromic substring. https://leetcode.com/problems/l ...
随机推荐
- HUD 1175 连连看
连连看 Time Limit : 20000/10000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submiss ...
- Vcenter 和ESXi License过期解决办法
Vcenter License过期解决办法 用client连接vcenter: KEY必须用vCenter Server 5 Standard序列号如果使用使用的“vCenter Server 5 ...
- 学习git最好的方式
1:登陆git官网网站 https://git-scm.com 2:点击esay to learn连接 3:点击Book连接 4:选择简体中文,下载PDF文档,也可以在线学习.
- Linux命令详解-文件系统管理
1. 外部设备简介 (1.)硬盘的分类: IDE硬盘 ./dev/hda hdb,hdc… 分区后:/dev/hda1 /dev/hda2 scsi硬盘: /dev/sda sdb,sdc ...
- 团队作业(二):ASB
团队作业(二):团队选题 题目四:基于Android的文件加密系统 系统名称:ASB 一.引言 1.1编写目的 (1)学习并熟悉掌握AES/DES加密算法的原理以及算法 (2)学习并熟悉Android ...
- Doris FE负载均衡配置
0 背景概述 Doris完全兼容了mysql协议,并且Doris FE本身通过多follower选举机制选举出master,可以保证fe本身的高可用性,也可以通过加入observer fe节点来提高f ...
- ZooKeeper系列(2) 安装部署 (转)
原文地址:http://www.cnblogs.com/wuxl360/p/5817489.html 一.Zookeeper的搭建方式 Zookeeper安装方式有三种,单机模式和集群模式以及伪集群模 ...
- mysql5.6修改字符编码,ERR:Illegal mix of collations for operation 'concat'
mysql5.6修改字符编码,ERR:Illegal mix of collations for operation 'concat' 1.问题起因:搭建环境初始化mysql的时候看到mysql配置文 ...
- javascript变量作用域 — 全局变量
javascript中,如果没有用var 声明一个变量,则该变量会被自动创建在全局作用域中,即使你是在某个函数中创建的,它也会成为全局变量,从而可以被另一个函数调用.
- XPath 常用语法札记
* 不包含属性的元素 例如不包含属性的span: span[not(@*)] * 文本包含某部分的元素 例如文本包含Rank的元素: *[contains(text(),'Rank')] * 选择匹配 ...