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.

  分析:   DP 问题

Initial state:
table[i][i] =  true.
table[i][i+1] = (s[i]==s[i+1]);

State Change:
if s[i]==s[j], table[i][j]=table[i+1][j-1]

class Solution {
public:
string longestPalindrome(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int n = s.size();
if(n < ) return s;
vector<vector<bool>> table(n, vector<bool>(n,false)); //init length 1
for(int i = ; i< n; i++)
table[i][i] = true; int len, maxlen = , maxStart = ,i,j;
for(len = ; len <= n ; len ++)
{
for(i = ; i< n - len + ; i++)
{
j = i + len - ; if(s[i] == s[j] &&len == )
{
table[i][j] = true;
maxlen = len;
maxStart = i;
} else if (s[i] == s[j] && table[i+][j-])
{
table[i][j] = true;
maxlen = len;
maxStart = i;
} }
} return s.substr(maxStart , maxlen);
}
};

这里解释下为什么len ==2 要单独处理: 因为table[i][j]只有上三角的值有意义,即 j >= i ; 当len = 2 时,table[i+1][j-1] j-1= i+2-1-1 = i  即此时j-1< i+1 ; 所以要单独处理

reference :http://leetcode.com/2011/11/longest-palindromic-substring-part-i.html

http://www.geeksforgeeks.org/dynamic-programming-set-12-longest-palindromic-subsequence/

LeetCode_Longest Palindromic Substring的更多相关文章

  1. 最长回文子串-LeetCode 5 Longest Palindromic Substring

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

  2. leetcode--5. Longest Palindromic Substring

    题目来自 https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...

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

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

  4. No.005:Longest Palindromic Substring

    问题: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...

  5. Leetcode Longest Palindromic Substring

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

  6. 【leedcode】 Longest Palindromic Substring

    Given a , and there exists one unique longest palindromic substring. https://leetcode.com/problems/l ...

  7. [LeetCode_5] Longest Palindromic Substring

    LeetCode: 5. Longest Palindromic Substring class Solution { public: //动态规划算法 string longestPalindrom ...

  8. 5. Longest Palindromic Substring

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

  9. leetcode-【中等题】5. Longest Palindromic Substring

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

随机推荐

  1. Codeforces 414B Mashmokh and ACM

    http://codeforces.com/problemset/problem/414/B 题目大意: 题意:一个序列B1,B2...Bl如果是好的,必须满足Bi | Bi + 1(a | b 代表 ...

  2. PowerShell中的输出

    1 输出重定向 >  或者>> 2 输出控制 out-* -paging#分页输出 get-process | out-host -paging more指令用于屏显 get-pro ...

  3. bootargs中的环境变量说明和一些常用的uboot命令

    bootargs中的环境变量说明和一些常用的uboot命令 一些常见的uboot命令:Help [command]在屏幕上打印命令的说明Boom [addr]启动在内存储器的内核Tftpboot通过t ...

  4. 利用sql 存储过程把表中内容自动生成insert语句

    选中所在数据库 执行创建存储过程的sql CREATE proc [dbo].[spGenInsertSQL] (@tablename nvarchar(256),@sqlwhere varchar( ...

  5. GIT入门篇-基本概念与操作

    GIT 首先必须说明的是, 这篇文章不是阐述GIT原理性和比较深入的文章.只是对于日常开发中比较常用的需求的总结和GIT这些命令大体的原理解释.所以掌握这个只能说能够应付一定的开发需求.但是如果你是个 ...

  6. WPF发布程序后未授予信任的解决办法

    WPF发布程序后未授予信任的解决办法 基于浏览器的WPF应用程序由于需要比较高的操作权限,所以在项目的安全性属性中选择了“这是完全可信的应用程序”选项.可是,在发布部署后,在其他电脑上打开xbap文件 ...

  7. WPF最基本的4个引用

    Windowsbase Windows基本类库 PresentationCore wpf核心类库 PresentationFramework wpf框架 System.Axml 系统类库

  8. wxpython 布局管理

    一个典型的应用程序是由不同的部件.这些小部件被放进容器部件.一个程序员必须管理应用程序的布局.这不是一项容易的任务.在wxPython我们有两个选择. *absolute positioning*si ...

  9. Uiviewcontroller 控制器的生命周期

    这是一个ViewController完整的声明周期,其实里面还有好多地方需要我们注意一下: 1:initialize函数并不会每次创建对象都调用,只有在这个类第一次创建对象时才会调用,做一些类的准备工 ...

  10. xcode6 建立 empty application

    .新建一个single view application .打开 Info.plist,删除里面的 Launch screen interface file....以及 Main storyboard ...