L【leetcode】ongest Palindromic Substring

Longest Palindromic Substring

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.

Hide Tags

String

利用DP思想做

dp[i][j]表示了第字符串中,s[i,i+1,……, j]是否是回文

可以有以下递推公式:

if(i==j) dp[i][j]=true;

if(i-j=1)dp[i][j]=s[i]==s[j];

if(i-j>1)dp[i][j]=dp[i+1][j-1]&&s[i]==s[j]

 1 class Solution {
 2 public:
 3     string longestPalindrome(string s) {
 4
 5         int n=s.length();
 6          //采用vector会超时
 7         //vector<vector<bool> > dp(n,vector<bool>(n));
 8         bool dp[1000][1000]={false};
 9         int maxDis=-1;
10         int start;
11         int end;
12         int dis;
13         for(int i=n-1;i>=0;i--)
14         {
15             for(int j=i;j<n;j++)
16             {
17                 if(s[i]==s[j])
18                 {
19                     dis=j-i;
20                     if(dis==0||dis==1||dp[i+1][j-1]&&dis>1)
21                     {
22                         dp[i][j]=true;
23                         if(dis>maxDis)
24                         {
25                             maxDis=dis;
26                             start=i;
27                             end=j;
28                         }
29                     }
30                 }
31             }
32         }
33         return s.substr(start,end-start+1);
34     }
35 };

一种O(n)的算法:

算法的思想就是根据前面串的对称情况,在寻找后面的对称串时,可以跳过不必要的比较

先在每两个相邻字符中间插入一个分隔符,当然这个分隔符要在原串中没有出现过。一般可以用‘#’分隔。这样就非常巧妙的将奇数长度回文串与偶数长度回文串统一起来考虑了(见下面的一个例子,回文串长度全为奇数了)

在比较的时候为了防止越界,可以再在字符串左右两边添加"^"和‘$‘

例如abb

^#a#b#b#$

P[id]记录的是以字符str[id]为中心的最长回文串,当以str[id]为第一个字符,这个最长回文串向右延伸了P[id]个字符

这里有一个很好的性质,P[id]-1就是该回文子串在原串中的长度(包括‘#’)

我们用mx记在i之前的回文串中,延伸至最右端的位置。同时用id这个变量记下取得这个最优mx时的id值。

如果i正好在某个回文串内部,则寻找与他对称的位置的元素的回文串的长度p[j],如果p[j]没有超出改回文串的内部,如下图所示,那么p[i]=p[j]

如果超出了,则取p[i]=mx-i

 1 class Solution {
 2
 3 public:
 4     string longestPalindrome(string s) {
 5
 6         int n=s.length();
 7         int id=0;
 8         int right=0;
 9         int i;
10
11         string pre_s;
12         pre_s="^#";
13         for(int i=0;i<n;i++)
14         {
15             pre_s=pre_s+s[i]+‘#‘;
16         }
17         pre_s+=‘$‘;
18
19
20         int n1=pre_s.length();
21
22         vector<int> p(n1);
23
24         for(i=1;i<n1-1;i++)
25         {
26             if(i<right)
27             {
28                 p[i]=min(p[2*id-i],right-i);
29             }
30             else
31             {
32                 p[i]=1;
33             }
34
35             while(pre_s[i+p[i]]==pre_s[i-p[i]])
36             {
37                 p[i]++;
38             }
39
40             if(right<p[i]+i)
41             {
42                 right=p[i]+i;
43                 id=i;
44             }
45         }
46
47         int index=0;
48         int maxLen=0;
49         for(i=0;i<n1-1;i++)
50         {
51             if(p[i]>maxLen)
52             {
53                 maxLen=p[i]-1;
54                 index=i;
55             }
56         }
57         //注意此处找到原来串的位置
58         return s.substr((index-maxLen-1)/2,(maxLen));
59
60     }
61 };
时间: 2024-08-11 05:35:30

L【leetcode】ongest Palindromic Substring的相关文章

【LeetCode】Longest Palindromic Substring 解题报告

DP.KMP什么的都太高大上了,自己想了个朴素的遍历方法. [题目] 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(n)吧) 从中间向两端搜索,分别找到以每个字母为中心的最长

【Leetcode】Longest Palindromic Substring

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 { 2 public: 3 string longestPalind

【leetcode】Longest Palindromic Substring (middle) 经典

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

【leetcode】Minimum Window Substring

问题: 给定两个字符串,S,T,返回S中包含T中所有字符的最短的字串,若不存在,则返回"".时间复杂度为O(n). 例如:S = "ADOBCODEBANC" T = "ABC" 返回BANC 生活场景: 把问题具体化现实化一点.有n层楼,每层楼里放有一个物品,现在老板给你一个物品清单,里面是要你集齐的物品,你可以乘坐电梯,但是电梯只停一次,停在哪一层,就从哪一层开始向楼上搜集物品,至于要在那层停电梯,由你自己选择. 这里我们当然选择即能集齐物品

LeetCode【5】. Longest Palindromic Substring --java实现

Longest Palindromic Substring 一.题目如下:        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. 题目要求给定字符串的最大对称子字符串,如"aaabccbac

Leetcode:【DP】Longest Palindromic Substring 解题报告

Longest Palindromic Substring -- HARD 级别 Question SolutionGiven 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题目. 主页君给出3种解

【leedcode】 Longest Palindromic Substring

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. https://leetcode.com/problems/longest-palindromic-substring/ 求最大回文的长度,其实这道题

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

原文地址: http://www.cnblogs.com/zhxshseu/p/4947609.html%20 转载请注明出处:http://www.cnblogs.com/zhxshseu/p/4947609.html 问题描述:Given a string S, find the longest palindromic substring in S. 这道题目是一个经典的动态规划DP http://challenge.greplin.com/问题,在面试中经常会被问到.为什么?因为这个问题可

【HDOJ】4426 Palindromic Substring

综合性很强的一道题目,结合manacher,后缀数组,哈希,RMQ,二分可解.基本思路是通过manacher可以找到所有可能的回文串,哈希去重,后缀数组二分找数目.最后暴力求解.需要注意kth需要为__int64. 1 /* 4426 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #in