标签:c++ leetcode python java 算法
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)
Github:
https://github.com/illuz/leetcode
题目:https://oj.leetcode.com/problems/Longest-Palindromic-Substring/
代码(github):https://github.com/illuz/leetcode
求一个字符串中的最长回文子串。
回文的解法有不少:
dp[i][j] = dp[i + 1][j - 1] (if s[i] == s[j])
本题要用第三种算法。
需要注意的是, Python 和 Java 的字符串和 C++ 的不一样,没有 \0
结尾,用’Manacher’s ALGORITHM’的时候是不一样的。
C++:
class Solution { public: string longestPalindrome(string s) { int p[N<<1]; string t = "$"; for (char ch : s) { t += '#'; t += ch; } t += '#'; // t为处理过的字符串,p为记录长度的数组 memset(p, 0, sizeof(p)); // mx为已判断回文串最右边位置,id为中间位置,mmax记录p数组中最大值 int mx = 0, id = 0, mmax = 0; int len = t.length(); int right = 0; for (int i = 1; i < len; i++) { p[i] = mx > i ? min(p[2 * id - i], mx - i) : 1; while (t[i + p[i]] == t[i - p[i]]) p[i]++; if (i + p[i] > mx) { mx = i + p[i]; id = i; } if (mmax < p[i]) { mmax = p[i]; right = i; } } // 最长为mmax - 1 return s.substr(right/2 - mmax/2, mmax-1); } };
Java:
public class Solution { public String longestPalindrome(String s) { int[] p = new int[2048]; StringBuilder t = new StringBuilder("$"); for (int i = 0; i < s.length(); ++i) { t.append('#'); t.append(s.charAt(i)); } t.append("#_"); // mx为已判断回文串最右边位置,id为中间位置,mmax记录p数组中最大值 int mx = 0, id = 0, mmax = 0; int right = 0; for (int i = 1; i < t.length() - 1; i++) { p[i] = mx > i ? Math.min(p[2 * id - i], mx - i) : 1; while (t.charAt(i + p[i]) == t.charAt(i - p[i])) p[i]++; if (i + p[i] > mx) { mx = i + p[i]; id = i; } if (mmax < p[i]) { mmax = p[i]; right = i; } } // 最长为mmax - 1 return s.substring(right/2 - mmax/2, right/2 - mmax/2 + mmax-1); } }
Python:
class Solution: # @return a string def longestPalindrome(self, s): t = '$#' + '#'.join(s) + '#_' p = [0] * 4010 mx, id, mmax, right = 0, 0, 0, 0 for i in range(1, len(t) - 1): if mx > i: p[i] = min(p[2 * id - i], mx - i) else: p[i] = 1 while t[i + p[i]] == t[i - p[i]]: p[i] += 1 if i + p[i] > mx: mx = i + p[i] id = i if mmax < p[i]: mmax = p[i] right = i return s[right//2 - mmax//2: right//2 - mmax//2 + mmax - 1]
[LeetCode] 005. Longest Palindromic Substring (Medium) (C++/Java/Python)
标签:c++ leetcode python java 算法
原文地址:http://blog.csdn.net/hcbbt/article/details/43975453