Middle-题目106:5. Longest Palindromic Substring

原创 2016年05月31日 19:57:47

题目原文:
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.
题目大意:
求出字符串S的最长回文子串。
题目分析:
使用了discuss的神算法,我根本看不懂……求大神解答……(好像还是个O(n2)的算法)大概是每向右增加一个字符,就判断能不能向左延伸之类的。
本题有一个线性算法,叫manacher算法,参见https://en.wikipedia.org/wiki/Longest_palindromic_substring#CITEREFManacher1975
源码:(language:java)
方法一:

public class Solution {
    public String longestPalindrome(String s) {
        char[] ca = s.toCharArray();
        int rs = 0, re = 0;
        int max = 0;
        for(int i = 0; i < ca.length; i++) {
            if(isPalindrome(ca, i - max - 1, i)) {
                rs = i - max - 1; re = i;
                max += 2;
            } else if(isPalindrome(ca, i - max, i)) {
                rs = i - max; re = i;
                max += 1;
            }
        }
        return s.substring(rs, re + 1);
    }

    private boolean isPalindrome(char[] ca, int s, int e) {
        if(s < 0) return false;

        while(s < e) {
            if(ca[s++] != ca[e--]) return false;
        }
        return true;
    }
}

方法二:(manacher算法,改编自wikipaedia上的代码)

public class Solution {
    public String longestPalindrome(String s) {
        if (s==null || s.length()==0)
            return "";
        char[] s2 = addBoundaries(s.toCharArray());
        int[] p = new int[s2.length]; 
        int c = 0, r = 0; // Here the first element in s2 has been processed.
        int m = 0, n = 0; // The walking indices to compare if two elements are the same
        for (int i = 1; i<s2.length; i++) {
            if (i>r) {
                p[i] = 0; m = i-1; n = i+1;
            } else {
                int i2 = c*2-i;
                if (p[i2]<(r-i)) {
                    p[i] = p[i2];
                    m = -1; // This signals bypassing the while loop below. 
                } else {
                    p[i] = r-i;
                    n = r+1; m = i*2-n;
                }
            }
            while (m>=0 && n<s2.length && s2[m]==s2[n]) {
                p[i]++; m--; n++;
            }
            if ((i+p[i])>r) {
                c = i; r = i+p[i];
            }
        }
        int len = 0; c = 0;
        for (int i = 1; i<s2.length; i++) {
            if (len<p[i]) {
                len = p[i]; c = i;
            }
        }
        char[] ss = Arrays.copyOfRange(s2, c-len, c+len+1);
        return String.valueOf(removeBoundaries(ss));
    }
    private  char[] addBoundaries(char[] cs) {
        if (cs==null || cs.length==0)
            return "||".toCharArray();

        char[] cs2 = new char[cs.length*2+1];
        for (int i = 0; i<(cs2.length-1); i = i+2) {
            cs2[i] = '|';
            cs2[i+1] = cs[i/2];
        }
        cs2[cs2.length-1] = '|';
        return cs2;
    }
    private  char[] removeBoundaries(char[] cs) {
        if (cs==null || cs.length<3)
            return "".toCharArray();

        char[] cs2 = new char[(cs.length-1)/2];
        for (int i = 0; i<cs2.length; i++) {
            cs2[i] = cs[i*2+1];
        }
        return cs2;
    }    
}

成绩:
方法一:9ms,beats 96.88%,众数13ms,4.50%
方法二:11ms,beats 92.14%
cmershen的碎碎念:
Manacher, Glenn (1975), “A new linear-time “on-line” algorithm for finding the smallest initial palindrome of a string”, Journal of the ACM 22 (3): 346–351, doi:10.1145/321892.321896.
这篇论文详细论证了Manacher算法,如果无聊可以研究一下。
方法二不如方法一快是因为数据量不够大(S.length<10^3),且此题个人认为应归为hard。

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

相关文章推荐

LeetCode题目 5. Longest Palindromic Substring

题目地址:点击打开链接  题目描述: Given a string s, find the longest palindromic substring in s. You may assume ...

LeetCode算法题目:Longest Palindromic Substring

题目: Given a string s, find the longest palindromic substring in s. You may assume that the maximum...

Leetcode 5. Longest Palindromic Substring

题目:Given a string s, find the longest palindromic substring in s. You may assume that the maximum le...

【LeetCode 5】 Longest Palindromic Substring【M】

1.动态规划法 2.Manacher 方法

[leetcode] 5-Longest Palindromic Substring

Palindromic Substring:回文子串,即一个字符串从左向右写和从右向走写是一样的,或者说字符串是对称。 解决思路:遍历字符串,依次找到所有回文子串的中间位置,分别尝试往两边扩展到最大...

Leetcode #5 Longest Palindromic Substring 最长回文串 解题小节

1 题目理解Longest Palindromic Substring 这道题是ACM一众的在线编程OJ系统里面的常客,最长回文串。很多题目都和这道题十分的类似,因此也具有很强的参考性和经典型。什么...

算法w3——Longest Palindromic Substring(leetcode 5)

题目: Longest Palindromic Substring 链接:https://leetcode.com/problems/longest-palindromic-substring/?t...

LeetCode 5.Longest Palindromic Substring的DP解法

题目描述: Given a string s, find the longest palindromic substring in s. You may assume that th...

LeetCode 5. Longest Palindromic Substring 解题报告

LeetCode 5. Longest Palindromic Substring 解题报告题目描述Given a string s, find the longest palindromic sub...

<LeetCode><Medium> 5 Longest Palindromic Substring #爆发式搜索

Given a string S, find the longest palindromic substring in S. You may assume that the maximum len...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)