Force deduction questions: 5. The longest palindrome substring

Subject requirements

Give you a string s and find the longest palindrome substring in s.
(Palindrome string is a string that is read both forward and backward)

  • 1 <= s.length <= 1000
  • s consists only of numbers and English letters (uppercase and/or lowercase)

Version 1: An algorithm that relies on intuition but overtime

class Solution {
    
    
public:
    string longestPalindrome(string s) {
    
    
        unsigned maxSize = 0;
        string maxHuiWen;
        for (size_t i = 0; i < s.length(); i++)
        {
    
    
            for (size_t j = i+1; j <= s.length(); j++)
            {
    
    
                if ((j-i > maxSize) && isHuiWen(s.substr(i, j-i)))
                {
    
    
                    maxSize = j - i;
                    maxHuiWen = s.substr(i, j - i);
                }
            }
        }
        return maxHuiWen;
    }
    bool isHuiWen(const string &s)
    {
    
    
        if (string(s.rbegin(), s.rend()) == s)
        {
    
    
            return true;
        }
    	return false;
    }
};

the whole idea

First construct a function to determine whether the string is a palindrome. For a given string s, find all the substrings of the s, call the palindrome judgment function to find the longest palindrome substring.

Version two: dynamic programming

class Solution {
    
    
public:
    string longestPalindrome(string s) {
    
    
        int lenth = s.length(), maxlenth = 0;
        string maxHuiWen;
        //初始化dp表
        bool** dp;
        dp = new bool*[lenth];
        for (size_t i = 0; i < lenth; i++)
        {
    
    
            dp[i] = new bool[lenth];
        }
        for (size_t i = 0; i < lenth; i++)
        {
    
    
            for (size_t j = i; j < lenth; j++)
            {
    
    
                if (i == j)
                {
    
    
                    dp[i][j] = true;
                }
                else
                    dp[i][j] = false;
            }
        }
        //当使用递减for循环时,不能使用unsigned i>=0;
        for (int i = lenth-1; i >= 0; i--)
        {
    
    
            for (int j = lenth-1; j >= i; j--)
            {
    
    
                //首尾相同
                if (s[i] == s[j])
                {
    
    
                    //子串是回文串 或者 没有子串
                    if (i+1 >= j-1 || dp[i+1][j-1] == true)
                    {
    
    
                        dp[i][j] = true;
                        if (j - i + 1 > maxlenth)
                        {
    
    
                            maxlenth = j - i + 1;
                            maxHuiWen = s.substr(i, j - i + 1);
                        }
                    }
                }
            }
        }
        return maxHuiWen;
    }
};

the whole idea

For a palindrome string, its beginning and end characters are the same. And after removing the first and last characters, the remaining character string is still a palindrome. Use this property for dynamic planning.
First initialize the dp table, and set the substring of length 1 in the given string as a palindrome string, which is true.
State transition: for the character substring of str[i,j] (from the beginning of i to the end of j). If the characters at the i-th position and the j-th position are the same at the beginning and end, and the substring from i+1 to j-1 is a palindrome, then the substring is a palindrome.
Start the construction from the end of the dp table to the head end, and finally complete the construction of the entire dp table.

What learned

1. Re-familiar with the idea of ​​dynamic programming and the construction of dp table.
2. For the decreasing for loop, you cannot use unsigned i and i>0 to control the end of the loop. Because unsigned is always greater than 0. At this time, you can only use int to control the end of the for loop.
3. Dynamic application method of two-dimensional array:

bool** dp;
dp = new  bool*[lenth]; //提前给定一个lenth
for (size_t i = 0; i < lenth; i++)
{
    
    
	dp[i] = new bool[lenth];
}

Guess you like

Origin blog.csdn.net/youyadefeng1/article/details/113404103