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 -- 最长回文字串的更多相关文章

  1. 【LeetCode每天一题】Longest Palindromic Substring(最长回文字串)

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  2. Leetcode5.Longest Palindromic Substring最长回文字串

    给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为1000. 示例 1: 输入: "babad" 输出: "bab" 注意: &quo ...

  3. [LeetCode] Longest Palindromic Substring 最长回文串

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  4. Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)

    Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...

  5. [LeetCode] 5. Longest Palindromic Substring 最长回文子串

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  6. 1. Longest Palindromic Substring ( 最长回文子串 )

    要求: Given a string S, find the longest palindromic substring in S. (从字符串 S 中最长回文子字符串.) 何为回文字符串? A pa ...

  7. LeetCode:Longest Palindromic Substring 最长回文子串

    题目链接 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  8. lintcode :Longest Palindromic Substring 最长回文子串

    题目 最长回文子串 给出一个字符串(假设长度最长为1000),求出它的最长回文子串,你可以假定只有一个满足条件的最长回文串. 样例 给出字符串 "abcdzdcab",它的最长回文 ...

  9. 【翻译】Longest Palindromic Substring 最长回文子串

    原文地址: http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-i.html 转载请注明出处:http:// ...

随机推荐

  1. Maven开发基础总结(Maven自启动,Maven打war包,Maven热部署)

    学习内容: 1.不依赖外部Tomcat,自己启动方式部署 2.Maven打war包,远程部署到centOS 3.Maven热部署(不关闭Tomcat部署应用)   做maven开发前提: 1.编码UT ...

  2. luabind 导出string问题

    luabind导出字符串 不能导出char* 会有问题 应该是字符串连接的时候出错了 static _TCHAR* pRetChar = new _TCHAR[10]; memcpy(pRetChar ...

  3. Nginx基础知识之————什么是 Nginx?

    本课时主要给大家讲解什么是 Nginx 和 Nginx 的功能,Nginx 与其他服务器的性能比较和 Nginx 的优点总结的知识,并结合实例让学员深入理解 Nginx 和 Nginx 的功能以及 N ...

  4. openerp安装记录及postgresql数据库问题解决

    ubuntu-14.04下openerp安装记录1.安装PostgreSQL 数据库    a.安装         sudo apt-get install postgresql    安装后ubu ...

  5. Git开源项目工作流程图

  6. 僵尸进程学习 & 进程状态列表 & Linux信号学习

    参考这篇文章: http://www.mike.org.cn/articles/treatment-of-zombie-processes-under-linux/ 在Linux进程的状态中,僵尸进程 ...

  7. Android开发面试经——6.常见面试官提问Android题②(更新中...)

    版权声明:本文为寻梦-finddreams原创文章,请关注:http://blog.csdn.net/finddreams 关注finddreams博客:http://blog.csdn.net/fi ...

  8. maven 启动 报错 Fatal error compiling: 无效的目标发行版

    http://news.tuxi.com.cn/news/119999990123162/31622105.html http://lyking2001.iteye.com/blog/837440 针 ...

  9. 数据库事物四大特性-ACID

    事务的:原子性.一致性.分离性.持久性 事物(transaction)是由一些列操作序列构成的执行单元,这些单元要么都做,要么不做,是一个不可分割的工作单元. 数据库事物的四个基本性质(ACID) 1 ...

  10. GreenPlum简单性能测试与分析

    版权声明:本文由黄辉原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/195 来源:腾云阁 https://www.qclou ...