您的位置:首页 > 编程语言 > C语言/C++

[LeetCode] 005. Longest Palindromic Substring (Medium) (C++/Java/Python)

2015-02-28 00:09 597 查看
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)

Github: https://github.com/illuz/leetcode

005.Longest_Palindromic_Substring (Medium)

链接

题目:https://oj.leetcode.com/problems/Longest-Palindromic-Substring/

代码(github):https://github.com/illuz/leetcode

题意

求一个字符串中的最长回文子串。

分析

回文的解法有不少:

暴力搜索 O(n^3)
动态规划 O(n^2),
dp[i][j] = dp[i + 1][j - 1] (if s[i] == s[j])

Manacher’s ALGORITHM可达到 O(n) 时间。

本题要用第三种算法。

需要注意的是, 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]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: