leetcode 5869. Maximum product of two palindrome subsequence lengths [shape pressure DP]

Give you a string s. please find two disjoint palindrome subsequences in s to maximize the product of their lengths. Two subsequences do not intersect if they do not have any characters with the same subscript in the original string.

Please return the maximum product of the length of two palindrome subsequences.

Subsequence refers to the result obtained by deleting several characters (one can be deleted) from the original string without changing the order of the remaining characters. If a string is as like as two peas read back and read from the back, it is a palindrome string.

Example 1:
! [insert picture description here]( https://img-blog.csdnimg.cn/c75f6433c7d94e76840ef534934fd7c1.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6Zi_LuiNoy4=,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center = 50%x50%)

Enter: s = "leetcodeom"
Output: 9
Explanation: the best solution is to select "ete" as the first subsequence and "cdc" as the second subsequence.
Their product is 3 * 3 = 9.

Example 2:

Input: s = "bb"
Output: 1
Explanation: the best solution is to select "b" (the first character) as the first subsequence and "b" (the second character) as the second subsequence.
Their product is 1 * 1 = 1.
Example 3:

Input: s = "accbcaxxcxx"
Output: 25
Explanation: the best solution is to select "accca" as the first subsequence and "xxcxx" as the second subsequence.
Their product is 5 * 5 = 25.

Tips:

2 <= s.length <= 12
s contains only lowercase English letters.

Method 1: use binary to represent substring and enumerate all subsequences. There are pow(2,n) subsequences in total
(1) Find all palindrome subsequences and save its binary representation. Enumerate each binary number, use the and operation to judge the position with the value of 1, so as to obtain the subsequence, and judge whether the subsequence is a palindrome,
(2) Enumerate all palindrome subsequences in pairs, judge whether two palindrome subsequences intersect with and operation, that is, whether the sum operation of binary value is equal to 0, and update the maximum product of length in real time.

code:

class Solution:
    def maxProduct(self, s: str) -> int:
        n = len(str)
        # Binary representation of palindrome subsequence
        valid = [0] * (1 << n)
        for i in range(1 << n):  # Enumerate the binary values of all subsequences, 1 < < n in total, n bits
            tmp_str = ''
            for j in range(n):
                if i & (1 << j):  # Judge whether the j-th bit of the right number is 1
                    tmp_str += s[n - i - 1]
            if tmp_str[::-1] == tmp_str:
                valid.append(len(tmp_str))
        # Enumeration subset
        maxp = 0
        for mask in range(1 << n):
            subject = i
            while subject:
                maxp = max(maxp, valid[subject] * valid[mask ^ subject])
                subject = (subject - 1) & mask
        return maxp

Method 2: state pressure DP + enumeration subset
The maximum length of the string is 12 bits. Each bit can be taken or not. There are pow(2, n) subsequences in total. Using the state compression method, 1 means to take the bit string, 0 means not to take the bit string, and the binary representation of each sub sequence represents a possible sub sequence.

valid is used to store the length of the subsequence when the subsequence of the ith binary representation is a palindrome subsequence.

code:

class Solution:
    def maxProduct(self, s: str) -> int:
        n = len(str)
        # Binary representation of palindrome subsequence
        valid = [0] * (1 << n)
        for i in range(1 << n):  # Enumerate the binary values of all subsequences, 1 < < n in total, n bits
            tmp_str = ''
            for j in range(n):
                if i & (1 << j):  # Judge whether the j-th bit of the right number is 1
                    tmp_str += s[n - i - 1]
            if tmp_str[::-1] == tmp_str:
                valid.append(len(tmp_str))
        # Enumeration subset
        maxp = 0
        for mask in range(1 << n):
            subject = i
            while subject:
                maxp = max(maxp, valid[subject] * valid[mask ^ subject])
                subject = (subject - 1) & mask
        return maxp

reference resources: https://leetcode-cn.com/problems/maximum-product-of-the-length-of-two-palindromic-subsequences/comments/

Tags: Algorithm leetcode Dynamic Programming

Posted on Sun, 12 Sep 2021 16:02:25 -0400 by nielskg