LeetCode5: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.

解题思路:

主要有三种:

第一种:Manacher算法,也是最快的,时间复杂度为O(n)

第二种:DP算法,时间复杂度为O(n*n)

第三种:中心法,时间复杂度为O(n*n)

实现代码:


#include <iostream>
#include <vector>
using namespace std;

/**

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.
*/

class Solution {
public:
//Manacher算法(O(n))
string longestPalindrome(string s) {
string p;
if(s.empty())
return p;
int id = 0;
int mx = 0;
//以下对要操作的副本str进行格式化,使得其为奇数
string str("^");
for(int i = 0; i < s.size(); i++)
{
str += "#";
str += s[i];
}
str += "#$";
vector<int> r(str.size(), 0);

for(int i = 1; i < str.size()-1; i++)
{
if(mx > i)
r[i] = min(r[2*id - i], mx - i);
else
r[i] = 1;
while(str[i+r[i]] == str[i-r[i]])//为了防止越界,str头尾插入了‘^‘和‘$‘字符
r[i]++;
if(r[i] + i > mx)
{
mx = r[i] + i;
id = i;
}
}

int maxlen = 0;
int maxid = 0;
for(int i = 1; i < str.size()-1; i++)
{
if(r[i] > maxlen)
{
maxlen = r[i];
maxid = i;
}
}
//(maxid-1)/2为原字符串中最长回文字串中心字符位置
//(maxlen-1)/2为原字符串中最长回文子串半径
//maxlen-1为原字符串中最长回文字串长度
return s.substr((maxid-1)/2 - (maxlen-1)/2, maxlen-1);

}

//DP O(n*n)
string longestPalindrome2(string s) {
string p;
if(s.empty())
return p;
int len = s.size();
vector<vector<bool>> dp(len, vector<bool>(len, false));//dp[i][j]表示i~j的字串是否为回文串
int maxstart = 0;
int maxlen = 1;
for(int i = 0; i < len-1; i++)
{
dp[i][i] = true;
if(s[i] == s[i+1])
{
dp[i][i+1] = true;
maxstart = i;
maxlen = 2;
}
}

for(int l = 3; l <= len; l++)
{
for(int i = 0; i < len-l+1; i++)
{
int j = i+l-1;
if(s[i] == s[j] && dp[i+1][j-1])
{
dp[i][j] = true;
maxstart = i;
maxlen = l;
}

}
}
return s.substr(maxstart, maxlen);

}

//中心法,以每一个字符作为回文串中心,向两边扩展
string longestPalindrome3(string s) {
string p;
if(s.empty())
return p;
int len = s.size();
int maxstart = 0;
int maxlen = 0;
for(int i = 0; i < len; i++)
{
int l = i-1;
int r = i+1;
int tmpmax = 1;//已i为中心的回文串:奇数
while(l >= 0 && r < len && s[l--] == s[r++])
tmpmax++;
if(maxlen < tmpmax*2 -1)
{
maxlen = tmpmax*2 -1;
maxstart = l+1;
}

int l2 = i;
int r2 = i+1;
int tmpmax2 = 0;//已i和i+1为中心的回文串,偶数时
while(l2 >= 0 && r2 < len && s[l2--] == s[r2++])
tmpmax2++;

if(maxlen < tmpmax2*2)
{
maxlen = tmpmax2*2;
maxstart = l2+1;
}

}

return s.substr(maxstart, maxlen);

}

};

int main(void)
{
string s("abbacdd");
Solution solution;
string p = solution.longestPalindrome3(s);
cout<<p<<endl;
return 0;
}

LeetCode5:Longest Palindromic Substring,码迷,mamicode.com

时间: 2024-08-26 20:11:22

LeetCode5:Longest Palindromic Substring的相关文章

LeetCode:Longest 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. 动态规划解法 T(n) = O(n^2)  ,S(n) = O(n^2); Solutio

LeetCode OJ: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. 玩了两天dota2,罪过罪过,还是应该老老实实刷题啊. 题目求得是最长的回文子串,这里使用的是暴力的解法,也就是解决两种回文"asdsa"以

Leetcode5:Longest Palindromic [email&#160;protected]

Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example: Input: "babad" Output: "bab" Note: "aba" is also a valid answer. Example: Input: "cbbd" Ou

LeetCode第[5]题(Java):Longest Palindromic Substring 标签:String、动态规划

题目中文:求最长回文子串 题目难度:Medium 题目内容: Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. 翻译: 给定一个字符串s,找出s中最长的回文子串.你可以假设s的最大长度是1000. 什么叫回文子串? 就是字符串中,满足能正读反读都一样的子串,就是回文子串.如下所示 Input: "babad"

LeetCode: Longest Palindromic Substring(Medium)

原题链接:https://leetcode.com/problems/longest-palindromic-substring/description/ 1. 题目要求:找出字符串中的最大回文子串 2. 注意:要考虑回文子串中的字符个数是奇数还是偶数!!! 例如,"aabaa"是一个奇数个字符的回文字符串,他的中心只有一个字符"b". "aabbaa"是一个偶数个字符的回文字符串,他的中心却有两个相同字符"bb" 3. 思路

leetcode5。Longest Palindromic Substring

写了一个爆搜,超时了,所以更改了一个方法,使用flag数组记录标志, 动态规划,类似于lcs的解法,数组flag[i][j]记录s从i到j是不是回文 首先初始化,i>=j时,flag[i][j]=true,这是因为s[i][i]是单字符的回文,当i>j时,为true,是因为有可能出现flag[2][1]这种情况,比如bcaa,当计算s从2到3的时候,s[2]==s[3],这时就要计算s[2+1] ?= s[3-1],总的来说,当i>j时置为true,就是为了考虑j=i+1这种情况. 接着

第五题:Longest Palindromic Substring

题目链接:题目链接 题意:找最长回文子串(注意不是回文序列,不一样,字串需要连续,序列不需要连续) 方法一: 这一题说实话,只想到最"土"的方法,就是找到所有的可能的串,然后得到最长的回文串. 需要注意:回文串有奇数和偶数之分,所以以当前这个点为中心,存在两种:这个点是最中间的点:这个点和之前一个点需要进行比较. 看代码如下: class Solution { public: string longestPalindrome(string s) { int max_len = 0, n

[Java]LeetCode5 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. 题意:求字符串中最长的回文 感觉这题不难,可以这样想,设置两个指针,分别对应0,len-1. 比如从第一个字符开始,abababac,我们可以找a出现

LeetCode-5. Longest Palindromic Substring(M)

解法 比较经典的问题,寻找最长回文子串.Leetcode里提供了多种解法.我采用最直观的解法:中心扩展法. 思路是每次以当前元素为中心向两边扩展,直到遇到不同元素,此时找到一个子串.有两点需要注意的地方: 1)空串和单字符都是回文,直接返回即可. 2)偶数回文和奇数回文的情况.例如:abace是aba的奇数串,以b为中心向两周扩展即可.但是abbawe是偶数串abba,这样需要以bb为指针向两周同时往外扩展. Given a string s, find the longest palindro