[LeetCode] 5. Longest Palindromic Substring palindromic longest substring

     The title given string request longest substring palindrome, the first conceivable method of use of force, to find the length of all the sub-set string palindromic sequence, whichever is longest substring, particularly sub-palindromic substring length is odd and the even length discussion, the time complexity of O (n ^ 2), but the method of solving this violence on leetcode timeout error will be reported, specific code as follows:

A. Violence Act

Reference Code

#include<string>
#include<string.h>
using namespace std;
class Solution {
public:
    string longestPalindrome(string s) {
       if(s.empty()||s.size()<=1) return s;
       int size = s.size();
       string odd_str = longestPalindrome_odd(s,size);
       string even_str = longestPalindrome_even(s,size); 
       return odd_str.size() >= even_str.size()?odd_str:even_str;
    }
    
    string longestPalindrome_odd(string& s,int size)
    {
       string res;
       for(int i = 0; i<size; ++i){
           string substr =  s.substr(i,1);
           for(int l = i-1,r = i+1;l>=0 && r<=size-1;l--,r++){
                if(s[l]!=s[r]) break;
                 else{
                   substr = s.substr(l,r-l+1);
                }
           }
           if(substr.size()>=res.size()) res = substr;
       }
       return res;
    }

    string longestPalindrome_even(string& s,int size)
    {
        string res; 
        for(int i = 0; i<size; ++i){
            string substr;
            for(int l = i,r = i+1;l>=0&&r<=size-1;l--,r++){
                if(s[l]!=s[r]) break;
                else{
                  substr = s.substr(l,r-l+1);
                }
            }
            if(substr.size()>=res.size()) res = substr;
        }
        return res;
    }
};

 

Two dynamic programming:

Step 1: Define state
    dp [i] [j] indicates whether the substring s [i, j] is a palindromic substring. First try to define the status of "What is the topic asked, what is set to put the state." Then consider "how the state transfer", if the "state transition equation" is not easy to get, try to modify the state definition, the purpose is to facilitate still get the "state transition equation."

 

Step 2: Thinking the state transition equation

   This step is doing Category Discussion (characters are equal according to the head and tail), to give an analysis of the status definitions:

dp [i] [j] = (s [i] == s [j]) and dp [i + 1] [j - 1]
Analysis of the state transition equation:

(1) "Dynamic programming" fill in fact a two-dimensional table, the relationship between i and j is the i <= j, and therefore, only the upper half of the need to fill this table;

(2) at s [i] == s [j] and the establishment of j - i <3 premise, a conclusion can be directly, dp [i] [j] = true, otherwise it performs a state transition.

  Note: The classification technique is discussed in turn transfer equation of state structure. Classification of state space, think about what the optimal substructure in the end yes. That is how big the problem is to get the optimal solution from the optimal solution to a small problem.

 

Step 3: Consider initialization

Initialization time, must be palindromic single character string initialized so the diagonal is 1, i.e., dp [i] [i] = 1.

In fact, the initialization of the parts are omitted. Because only when a certain character is palindromic, dp [i] [i] will not be a state other reference value.

Step 4: Consider the output
as long as a resulting dp [i] [j] = true, is recorded and the length of the substring starting position is not necessary to intercept, but also because the character string taken consumption performance, in this case the recording palindromic substring the "start position" and "palindrome length" button.

Step 5: if a compression state may be considered
as the process of filling, only the reference values of the lower left. In fact it can be compressed, but will increase the number of judges sentence, write and understand the code more difficult, loss readability. Here the state is not compressed.

The following is a matter of coding time to pay attention: the determination kid is always the first to get a palindrome string, then the big guy substring reference to the string of judgment.

The idea is:

1, in the course of the substring right boundary j gradually expanded, the location of the left edge of the enumeration may occur;

2, the left margin when you can enumerate from small to large, from large to small can be.

Reference code 2:

class Solution {
public:
    string longestPalindrome(string s) {
        const int size = s.size();
        if(s.empty()||size <= 1)
           return s;
        int l=0,h=0,seq=0;
       bool dp[size][size];
        for(int j = 1;j<size;++j)
           for(int i = 0;i<=j;++i)
            {
                int sub_seq = j-i+1;
                if(sub_seq<3)
                {
                    dp[i][j]=s[i]==s[j];
                }
                else
                {
                     dp[i][j]=(s[i]==s[j]&&dp[i+1][j-1]);
                }
                if(dp[i][j]&&sub_seq>=seq){
                    l=i;
                    h=j;
                    seq=sub_seq;
                }
            }
            return s.substr(l,seq);
    }
};

Guess you like

Origin www.cnblogs.com/wangxf2019/p/12157561.html