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. 关于group by 两个或以上条件的分析

    关于group by 两个或以上条件的分析     原文地址:http://uule.iteye.com/blog/1569262 博客分类: 数据库   首先group by 的简单说明: grou ...

  2. CentOS 7一些常用配置

    一.更改启动模式 背景:个人开发环境,安装了GNOME桌面,默认启动是图形化模式. 修改为命令行模式. systemctl set-default multi-user.target 二.命令行模式下 ...

  3. 搭建本地的git仓库

    折腾了快一天了,终于搭建成功了. 分享一下搭建的步骤: 一.GIT仓库的创建 1. adduser git 2. passwd git 此例设置git的密码为123456 3. cd /home/gi ...

  4. netstat -ano,查看已占用端口,结束已被占用的端口,ntsd,关闭任务管理器杀不了的进程

    cmd——回车,输入netstat -ano——回车,可以查看已占用的端口,记下端口的PID,然后打开任务管理器,点查看,选择列,勾选PID确定,找到对应的PID,结束进程,如果结束不了或者结束后还不 ...

  5. SpringMVC 服务器端验证

    1.导入JSR303验证类库Jar包2.在MVC的配置文件中添加<mvc:annotation-driven/>的配置3.在MVC的配置文件中添加验证器的配置4.在接收表单数据的类中添加验 ...

  6. [SAP ABAP开发技术总结]ABAP读写、解析XML文件

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  7. CUBRID学习笔记 13 日志文件

    欢迎转载 ,转载时请保留作者信息.本文版权归本人所有,如有任何问题,请与我联系wang2650@sohu.com . 过错 CUBRID Broker Log Files  可以理解为数据库中间件日志 ...

  8. chainOfResponsibility责任链模式

    责任链(Chain of Responsibility)模式 : 责任链模式是对象的行为模式.使多个对象都有机会处理请求,从而避免请求的发送者和接受者直接的耦合关系.将这些处理对象连成一条链,沿着这条 ...

  9. How To Use API Online?

    Example: 在线 java8 api 1.http://docs.oracle.com/javase/8/docs/api/ 2.ctrl+f 3.输入类名! 4.

  10. poj1434Fill the Cisterns!(二分)

    链接 题目说给你n个水箱,初始是没有水的,每个的高低位置可能不同,给了你初始的水箱底部所处的水平线位置,问给你V体积水时,水的水平线位置. 直接二分位置p,对于每一个底部低于水平线位置的水箱,里面的水 ...