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.

动态规划解法 O(n2) 超时

string longestPalindrome(string s) {
if(s.empty()) return s;
vector<vector<bool>> isPalindrome(s.length() + , vector<bool>(s.length() + , false));
string ans = s.substr(,);
for(int i = ; i <= s.length(); i++) //长度
{
for(int j = ; j <= s.length() - i; j++) //起始位置
{
if((i <= ) || (isPalindrome[j + ][i - ] && s[j] == s[j + i - ]))
{
isPalindrome[j][i] = true;
if(i > ans.length())
ans = s.substr(j, i);
}
}
}
return ans;
}

O(N)解法 AC

string longestPalindrome2(string s)
{
if(s.empty()) return s;
string ss = "#";
for(int i = ; i < s.length(); i++)
{
ss += s.substr(i, ) + "#";
}
vector<int> P(ss.length()); //记录新字符串以每一个字符为中心时 包括中心的一半长度 只有一个时长度为1
string ans = s.substr(,);
int mx = ; //向右最远的对称位置+1
int id = ; //mx对应的中心位置
for(int i = ; i < ss.length(); i++)
{
P[i] = (mx > i) ? min(P[*id - i], mx - i) : ;
while(i - P[i] >= && i + P[i] < ss.length() && ss[i - P[i]] == ss[i + P[i]]) P[i]++;
if(P[i] - > ans.length())
ans = s.substr((i - P[i] + )/, P[i] - );
if(i + P[i] > mx)
{
mx = i + P[i];
id = i;
}
}
return ans;
}

【leetcode】Longest Palindromic Substring (middle) 经典的更多相关文章

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

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

  2. Leetcode Longest Palindromic Substring

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

  3. [LeetCode] Longest Palindromic Substring(manacher algorithm)

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

  4. C++ leetcode Longest Palindromic Substring

    明天就要上课了,再过几天又要见班主任汇报项目进程了,什么都没做的我竟然有一种迷之淡定,大概是想体验一波熬夜修仙的快乐了.不管怎么说,每天还是要水一篇博文,写一个LeetCode的题才圆满. 题目:Gi ...

  5. Leetcode: Longest Palindromic Substring && Summary: Palindrome

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

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

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

  7. Leetcode: Longest Palindromic Substring. java

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

  8. LeetCode——Longest Palindromic Substring

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

  9. [LeetCode]Longest Palindromic Substring题解(动态规划)

    Longest Palindromic Substring: Given a string s, find the longest palindromic substring in s. You ma ...

随机推荐

  1. Quartz 第三课 More About Jobs & JobDetails(官方文档翻译)

    当学完第二课之后,你欣喜的发现,让jobs工作起来是还是相当简单的.虽然让jobs运行起来很简单,对于其执行的关键内容还是需要知道的.它们是IJob接口中的Execute和JobDetails. 当你 ...

  2. 关于百度编辑器UEditor(1.4.3)在C#.NET中的应用实例

    首先去百度UEditor官网下载 1.4.3 .net版本 http://ueditor.baidu.com/build/build_down.php?n=ueditor&v=1_4_3-ut ...

  3. Ubuntu 下 glpk 的安装及使用

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4156204.html glpk是一个开源的求解线性规划的包. 添加源: deb http:/ ...

  4. stl::find,find_if,find_if_not

    //满足特定条件下的实现,回调函数template<class InputIt, class UnaryPredicate> InputIt find_if(InputIt first, ...

  5. javascript显示倒计时控制按钮

    html: <a><span id="sendAgain" onclick="sendEmail()">2.再次发送激活邮件</s ...

  6. Js popup position which right under target item

    <div style="margin-left:600px;"> <div id="Span1" style="color:#eee ...

  7. mysql高可用探究 MMM高可用mysql方案

    1    MMM高可用mysql方案 1.1  方案简介 MMM即Master-Master Replication Manager for MySQL(mysql主主复制管理器)关于mysql主主复 ...

  8. dbt

    Procedure Relocate(s : state; b : base_index) { Move base for state s to a new place beginning at b ...

  9. php header函数要点

    发布:snowfly   来源:网络     [大 中 小] 相信很多人写程序时,使用 header(location) 进行跳转往往不记得写 exit() 语句,这种做法存在严重风险. 从浏览器来看 ...

  10. xaml中绑定单例属性

    在项目中经常会遇到,同一个字典表绑定到多个ItemsControl上的情况,可以在单例中创建一个List,xaml上绑定即可.看代码: 1,XAML <Grid> <StackPan ...