算法练习5. Longest Palindromic Substring 最长回文子字符串(Manacher算法)
来源:互联网 发布:金山数据恢复收费标准 编辑:程序博客网 时间:2024/06/07 14:17
5. Longest Palindromic Substring
https://leetcode.com/problems/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.
返回字符串中的最长回文子字符串
class Solution {
public:
string longestPalindrome(strings) {
string str;
str = changstr(s);
int len= str.length(),i;
int *p = new int[str.length() + 1];
memset(p, 0, sizeof(p));
int mx = 0, id = 1;
for (i = 1; i < len;i++)
{
if (mx > i)
{
p[i] = min( p[2 * id - i] , (mx- i));
}
else
{
p[i] = 1;
}
while (i - p[i]>0 &&i + p[i]<len&&str.at(i - p[i]) == str.at(i + p[i]))
p[i]++;
if (i + p[i] > mx)
{
mx = i + p[i];
id = i;
}
}
//最大回文字符串长度
int maxlen = 0, index =0;
for (int i = 0; i<len;i++)
{
if (p[i]>maxlen)
{
id = i;
maxlen = p[i];
}
}
string result;
for (i = id - p[id]+1;i < id+maxlen; i++)
{
if (str.at(i) != '#')
{
result += str.at(i);
}
}
return result;
}
string changstr(stringstr)
{
string str0;
str0 += "$#";
for (int i = 0; i <str.size(); i++)
{
str0 += str[i];
str0 += "#";
}
return str0;
}
};
以下内容来自LeetCode官网:Approach #4 (Expand Around Center)[Accepted]
In fact, wecould solve it in O(n^2)O(n2) time usingonly constant space.
We observe thata palindrome mirrors around its center. Therefore, a palindrome can be expandedfrom its center, and there are only 2n - 12n−1 suchcenters.
You might be asking why there are 2n - 12n−1 butnot nn centers? The reason is the centerof a palindrome can be in between two letters. Such palindromes have evennumber of letters (such as ''abba''''abba'') and its center are between thetwo 'b''b's.
public StringlongestPalindrome(String s) {
int start= 0, end= 0;
for(int i= 0; i< s.length(); i++) {
int len1= expandAroundCenter(s,i, i);
int len2= expandAroundCenter(s,i, i+ 1);
int len= Math.max(len1, len2);
if (len > end - start) {
start = i- (len - 1)/ 2;
end = i + len / 2;
}
}
returns.substring(start, end+ 1);
}
privateintexpandAroundCenter(String s,int left,int right) {
int L= left, R = right;
while(L >= 0&&R < s.length()&& s.charAt(L) == s.charAt(R)) {
L--;
R++;
}
returnR - L - 1;
}
- 算法练习5. Longest Palindromic Substring 最长回文子字符串(Manacher算法)
- 浅谈manacher算法 最长回文子串(Longest Palindromic Substring)
- LeetCode5. Longest Palindromic Substring(最长回文子串:Manacher算法)
- manacher算法求最长回文子串(Longest Palindromic Substring)
- 【LeetCode】Longest Palindromic Substring(最长回文子串1)[马拉车Manacher算法]
- 【LeetCode】Manacher算法+Longest Palindromic Substring(最长回文子串)
- Longest Palindromic Substring最长回文字符串算法
- 5. Longest Palindromic Substring(求最长回文子字符串)
- Leetcode|Longest Palindromic Substring(最长回文的几种方法)(Manacher算法)
- Longest Palindromic Substring(最长回文子字符串)
- leetcode 5. Longest Palindromic Substring(回文和Manacher算法)
- 最长回文子字符串(Longest Palindromic Substring)
- Longest Palindromic Substring (最长回文串)【面试算法leetcode】
- 【LeetCode-面试算法经典-Java实现】【005-Longest Palindromic Substring(最长回文子串)】
- LeetCode 5. Longest Palindromic Substring(最长回文子串)
- LeetCode---5. Longest Palindromic Substring(最长回文子串)
- 5. Longest Palindromic Substring(最长回文子串)
- 【leetcode】5.Longest Palindromic Substring >>Manacher算法
- HDU 1358 (KMP)
- 动态规划 problem K
- 结构化与面向对象
- 《编程珠玑》备忘录(开篇)---位图排序扩展
- 折半查找
- 算法练习5. Longest Palindromic Substring 最长回文子字符串(Manacher算法)
- Can't connect to MySQL server on localhost (10061)解决方法
- 互联网+鸡汤=网红,致敬去伪存真的全媒体时代
- HDU 2092 算菜价(结束条件,坑呀!)
- mysql授权
- Android文本框振动效果的实现
- 2016"百度之星" - 测试赛(热身,陈题)-列变位法解密(模拟)
- SpringMVC提供@RestController Restful风格
- 二维数组中查找值