LeetCode "Longest Palindromic Substring" - 1D DP

2D DP is an intuitive solution of course, but I got an MLE error, so I simplified it into a 1D DP:

class Solution {
public:
    void goDp(vector<int> &dp, int &maxLen, int &is, int &ie, int startLen, int len, string &s)
    {
        for(int ilen = startLen; ilen < len; ilen += 2)
        for(int i = 0; i < len - ilen; i ++)
        {
            bool bEq = s[i] == s[i + ilen];
            dp[i] = bEq ? (dp[i + 1] > 0 ? dp[i + 1] + 2 : 0) :    0;
            if( dp[i] > maxLen)
            {
                maxLen = dp[i];
                is = i; ie = i + ilen;
            }
        }
    }
    string longestPalindrome(string s) {
        int len = s.length();
        if(len == 0) return "";

        int maxLen = -1; int is = 0, ie = 0;
        //     Init
        vector<int> dp; dp.resize(len);

        //    Starting from 2
        for(int i = 0; i < len - 1; i ++)
        {
            if(s[i] == s[i + 1]) dp[i] = 2;
            else dp[i] = 0;
            if( dp[i] > maxLen)
            {
                maxLen = dp[i];
                is = i; ie = i + 1;
            }
        }
        goDp(dp, maxLen, is, ie, 3, len, s);

        //    Starting from 1
        std::fill(dp.begin(), dp.end(), 1);
        goDp(dp, maxLen, is, ie, 2, len, s);

        return s.substr(is, ie - is + 1);
    }
};

Since loop on current length n depends linearly on dp data with length n-1, we can use 1D DP. And there are two cases of odd\even lengthes.

LeetCode "Longest Palindromic Substring" - 1D DP

时间: 2024-10-28 11:43:54

LeetCode "Longest Palindromic Substring" - 1D DP的相关文章

[LeetCode] Longest Palindromic Substring [14]

题目 Given a string S, find the longest palindromic substring in S. You may assume that the maximum length ofS is 1000, and there exists one unique longest palindromic substring. 原题链接 解题思路 最长回文字串,相信做过Palindrome Partitioning II 这个题的同学应该可以很快做出来.没错,这个题还可以

Leetcode: Longest Palindromic Substring. java

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. 动态规划public class Solution { public String longestPalindrome(String s) { if

Leetcode: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. 翻译:求给定

[Leetcode] 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. 做这道题之前要先了解什么是回文子串.回文串通俗的解释是,分别从字符串两端开始遍历,得到的结果相同,如"abba",从两端的遍历结果都是:&q

[leetcode]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. 基本思想: 本题的navie解法是对每个(i,j) i<=j 测试是否是回文串,并记录最大的情况.O(n^3) 如果从回文串的中心考虑往两

LeetCode——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. 给定一个字符串S,找出当中的最长回文字符子串. 1.枚举全部子串,并推断是否是回文串同一时候记录最大长度.超时. //找出全部子串,并推断是否是回文串

[LeetCode] 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. http://fisherlei.blogspot.com/2012/12/leetcode-longest-palindromic-substrin

5.Longest Palindromic Substring (String; DP)

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. 思路:KMP,一种字符串匹配方法. 首先在字符串的每个字符间加上#号.For example: S = “abaaba”, T = “#a#b#a#a

leetcode longest palindromic substring (medium) /java

最长回文字串 上题: 测试用例中,注意aaabaaaa. 但是我time limit exceeded.用了极暴力的方法解.(三层循环)找每一个字符的最长回文字串. 1 /** 2 * 最长回文子串 3 * 2017-5-7 4 **/ 5 6 import java.io.*; 7 import java.util.*; 8 import java.lang.*; 9 10 public class Solution 11 { 12 public static String longestPa