5. Longest Palindromic Substring -- 最长回文字串
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.
(1)
class Solution {
public:
string longestPalindrome(string s) {
if (s == "")
return "";
int l = s.length(), r[], maxID = , ID = , m = , i;
string ss;
ss.resize(l * + );
ss[] = '@';
ss[] = '#';
for (i = ; i < l; i++)
{
ss[i * + ] = s[i];
ss[i * + ] = '#';
}
ss[l * + ] = '\n';
l = l * + ;
for (i = ; i < l; i++)
{
if (maxID > i)
r[i] = min(r[(ID << ) - i], maxID - i);
else
r[i] = ;
while (ss[i + r[i]] == ss[i - r[i]])
r[i]++;
if ((i + r[i] - ) > maxID)
{
maxID = i + r[i] - ;
ID = i;
}
if (r[i] > r[m])
m = i;
}
return s.substr((m-r[m])>>, r[m]-);
}
};
(2)DP
string longestPalindrome_dp_opt_way(string s) {
int n = s.size();
if (n<=) return s;
//Construct a matrix, and consdier matrix[j][i] as s[i] -> s[j] is Palindrome or not.
// ------^^^^^^
// NOTE: it's [j][i] not [i][j]
//Using vector could cause the `Time Limit Error`
//So, use the native array
bool **matrix = new bool* [n];
int start=, len=;
// Dynamic Programming
// 1) if i == j, then matrix[i][j] = true;
// 2) if i != j, then matrix[i][j] = (s[i]==s[j] && matrix[i-1][j+1])
for (int i=; i<n; i++){
matrix[i] = new bool[i+];
memset(matrix[i], false, (i+)*sizeof(bool));
matrix[i][i]=true;
for (int j=; j<i; j++){
// The following if statement can be broken to
// 1) j==i, matrix[i][j] = true
// 2) the length from j to i is 2 or 3, then, check s[i] == s[j]
// 3) the length from j to i > 3, then, check s[i]==s[j] && matrix[i-1][j+1]
if ( i==j || (s[j]==s[i] && (i-j< || matrix[i-][j+]) ) ) {
matrix[i][j] = true;
if (len < i-j+){
start = j;
len = i-j+;
}
}
}
}
for (int i=; i<n; i++) {
delete [] matrix[i];
}
delete [] matrix;
return s.substr(start, len);
}
5. Longest Palindromic Substring -- 最长回文字串的更多相关文章
- 【LeetCode每天一题】Longest Palindromic Substring(最长回文字串)
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- Leetcode5.Longest Palindromic Substring最长回文字串
给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为1000. 示例 1: 输入: "babad" 输出: "bab" 注意: &quo ...
- [LeetCode] Longest Palindromic Substring 最长回文串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)
Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...
- [LeetCode] 5. Longest Palindromic Substring 最长回文子串
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- 1. Longest Palindromic Substring ( 最长回文子串 )
要求: Given a string S, find the longest palindromic substring in S. (从字符串 S 中最长回文子字符串.) 何为回文字符串? A pa ...
- LeetCode:Longest Palindromic Substring 最长回文子串
题目链接 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- lintcode :Longest Palindromic Substring 最长回文子串
题目 最长回文子串 给出一个字符串(假设长度最长为1000),求出它的最长回文子串,你可以假定只有一个满足条件的最长回文串. 样例 给出字符串 "abcdzdcab",它的最长回文 ...
- 【翻译】Longest Palindromic Substring 最长回文子串
原文地址: http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-i.html 转载请注明出处:http:// ...
随机推荐
- Trigger Execution Sequence Of Oracle Forms
Sequence of triggers fires on Commit.1. KEY Commit2. Pre Commit3. Pre/On/Post Delete4. Pre/On/Po ...
- [POJ1830]开关问题(高斯消元,异或方程组)
题目链接:http://poj.org/problem?id=1830 题意:中文题面,求的是方案数. 首先可以知道, 如果方案数不止一个的话,说明矩阵行列式值为0,即存在自由变元,由于变量只有两种状 ...
- ALV详解:OO ALV
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- phpcms 头部搜索栏上边的 “新闻 | 图片 | 下载 | 专题” 是在哪里修改的?
phpcms 是怎么修改以下 栏目列. 在后台的时候,“模块管理>全站搜索”,可以修改搜索分类
- tilemap坐标转换
像素点跟tile的索引之间的转换//从cocos2d-x坐标转换为Tilemap坐标CCPoint GameMap::tileCoordForPosition(CCPoint position){ i ...
- Ubuntu 通过Deb安装 MySQL5.5(转载)
1. 下载 MySQL 5.5 deb 安装包 cd /usr/local/src sudo wget -O mysql-5.5.22-debian6.0-i686.deb http://dev.my ...
- delegate基于on
前几天看到事件委托的时候,关于live()方法讲的不是很详细,就去搜了一下关于live()和delegate()的,最后看源码发现bind()和delegate()都是由on()实现的,感兴趣的朋友可 ...
- html 如何获取表格中所选行的一行数据,并赋值到对应的TEXT里面?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 设计js通用库
设计js通用库的四个步骤: 1.需求分析:分析库需要完成的所有功能. 2.编程接口:根据需求设计需要用到的接口及参数.返回值. 3.调用方法:支持链式调用,我们期望以动词方式描述接口. (ps:设计链 ...
- a链接onclick="window.location.href=在ie6上面无法执行解决
<a href="javascript:void(0)" onclick="window.location.href=document.getElementById ...