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. js 定位到某个锚点

    js 定位到某个锚点 html页面内可以设置锚点,锚点定义 <a name="firstAnchor">&nsbp;</a> 锚点使用 <a  ...

  2. Html basic tag

    The <p> tag defines a paragraph. http://www.w3schools.com/tags/tag_p.asp The <td> tag de ...

  3. 如何选择正确的DevOps工具

    坦白的讲:世界上没有哪种工具能够像DevOps这么神奇(或敏捷,或精益).DevOps在开发和运营团队之间建立了完美的合作与沟通,因此与其说这是一种神奇的工具,不如说是一种文化的转变. 然而,团队之间 ...

  4. 先来个xmpp学习连接

    http://my.oschina.net/SoulJa/blog?catalog=3340253&temp=1468228088114 http://my.oschina.net/iOSli ...

  5. 生产者-消费者 用非阻塞队列、Object.wait()、Object.notify()实现

    非阻塞队列,需要考虑到: 1.并发中的同步 2.线程间通信 public class Quene_Pro_Con { //定义队列大小 private static int size = 10; // ...

  6. getting started with building a ROS simulation platform for Deep Reinforcement Learning

    Apparently, this ongoing work is to make a preparation for futural research on Deep Reinforcement Le ...

  7. XML约束——DTD约束

    参考: 方立勋老师的讲课视频.   什么是XML约束 •在XML技术里,可以编写一个文档来约束一个XML文档的书写规范,这称之为XML约束. 为什么需要XML约束 常用的约束技术 •XML DTD • ...

  8. iOS - OC Struct 结构体

    1.结构体的定义与调用 // 定义结构体类型 // 结构体类型名为 MyDate1 struct MyDate1 { int year; int month; int day; }; // 定义结构体 ...

  9. (二)再议MII、RMII、GMII接口

    概述:         MII (Media Independent Interface(介质无关接口)或称为媒体独立接口,它是IEEE-802.3定义的以太网行业标准.它包括一个数据接口和一个MAC ...

  10. 【转载】Spark系列之运行原理和架构

    参考 http://www.cnblogs.com/shishanyuan/p/4721326.html 1. Spark运行架构 1.1 术语定义 lApplication:Spark Applic ...