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,一种字符串匹配方法。
![](http://www.leetcode.com/wp-content/uploads/2011/11/palindrome_table10.png)
- 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 ...
随机推荐
- Spring IOC - 控制反转(依赖注入) - 创建对象的方式
a.通过类的无参构造方法创建对象 在入门案例中就是这种方式.当用最普通的方式配饰一个<bean>时,默认就是采用类的 无参构造创建对象.在Spring容器初始化时,通过<bean&g ...
- js中replace的用法(两种常用举例,还有好多用法不一一列举)
1.替换特定字符 <html><body> <script type="text/javascript"> var str="Visi ...
- java 调用windows bat脚本
当我们需要在java程序中调用外部程序,我们可用通过Runtime.exec()调用来完成. The class java.lang.Runtime features a static method ...
- centos7 设置系统时间与网络同步
1.安装ntpdate工具 yum -y install ntp ntpdate 2.设置系统时间与网络时间同步 ntpdate cn.pool.ntp.org 3.将系统时间写入硬件时间 hwclo ...
- [UE4]roll pitch yaw
UE4中的定义: 一.Roll,绕着X轴旋转的角度 二.Pitch,绕着Y轴旋转的角度 三.Yaw,绕着Z轴旋转的角度 Rotator 一.(Roll,Pitch,Yaw) 二.Rotator(0,0 ...
- [UE4]FString常用API
转自:http://aigo.iteye.com/blog/2279808 将int或float转换为string: 将FString转换为char*: 将string转换为int或者float: 字 ...
- html文字在django模板中取消转译
django {{ news.content | safe }} 还有escape=fales,具体咋用,有点蒙蔽
- bootstrap3中select2的默认值和下拉框的禁用
最近做项目用到了select2插件,需求中需要给下拉框设置默认值之后,禁用下拉框,我开始的写法是这样的 <script type="text/javascript"> ...
- windows巡检
参考网站: http://www.jb51.net/os/windows/525017.html 系统自带工具巡检 : 先说说如何检查系统健康度的方法,Win+R只有只要输入一个命令: perf ...
- leetcode501
/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...