当前位置:网站首页>[730. statistics of different palindrome subsequences]

[730. statistics of different palindrome subsequences]

2022-06-10 10:10:00 Sugar_ wolf

source : Power button (LeetCode)

describe :

Given a string s, return s Different non empty in 「 Palindrome subsequence 」 Number .

By getting from s Delete in 0 One or more characters to obtain subsequences .

If a character sequence is consistent with the character sequence it reverses , So it's 「 Palindrome character sequence 」.

If there is one i , Satisfy ai != bi , Then two sequences a1, a2, ... and b1, b2, ... Different .

Be careful :

  • The results could be big , You need to 109 + 7 modulus .

Example 1:

 Input :s = 'bccb'
 Output :6
 explain :6  Different sequences of non empty palindromes are :'b', 'c', 'bb', 'cc', 'bcb', 'bccb'.
 Be careful :'bcb'  It appears twice but only counts once .

Example 2:

 Input :s = 'abcdabcdabcdabcdabcdabcdabcdabcddcbadcbadcbadcbadcbadcbadcbadcba'
 Output :104860361
 explain : share  3104860382  Different non empty palindrome subsequences ,104860361  Yes  109 + 7  The value after taking the mold .

Tips :

  • 1 <= s.length <= 1000
  • s[i] Contains only 'a', 'b', 'c' or 'd'

Method 1 : Dynamic programming ( Using 3D arrays )

Ideas and algorithms

 Insert picture description here

 Insert picture description here
 Insert picture description here
Code :

class Solution {
    
public:
    const int MOD = 1e9 + 7;

    int countPalindromicSubsequences(string &s) {
    
        int n = s.size();
        vector<vector<vector<int>>> dp(4, vector<vector<int>>(n, vector<int>(n, 0)));
        for (int i = 0; i < n; i++) {
    
            dp[s[i] - 'a'][i][i] = 1;
        }

        for (int len = 2; len <= n; len++) {
    
            for (int i = 0, j = len - 1; j < n; i++, j++) {
    
                for (char c = 'a', k = 0; c <= 'd'; c++, k++) {
    
                    if (s[i] == c && s[j] == c) {
    
                        dp[k][i][j] = (2LL + dp[0][i + 1][j - 1] + dp[1][i + 1][j - 1] + dp[2][i + 1][j - 1] + dp[3][i + 1][j - 1]) % MOD;
                    } else if (s[i] == c) {
    
                        dp[k][i][j] = dp[k][i][j - 1];
                    } else if (s[j] == c) {
    
                        dp[k][i][j] = dp[k][i + 1][j];
                    } else {
    
                        dp[k][i][j] = dp[k][i + 1][j - 1];
                    }
                }
            }
        }

        int res = 0;
        for (int i = 0; i < 4; i++) {
    
            res = (res + dp[i][0][n - 1]) % MOD;
        }
        return res;
    }
};

Execution time :260 ms, In all C++ Defeated in submission 45.32% Users of
Memory consumption :153.2 MB, In all C++ Defeated in submission 10.84% Users of
 Insert picture description here

Method 2 : Dynamic programming ( Use a two-dimensional array )

Ideas and algorithms

 Insert picture description here
 Insert picture description here
 Insert picture description here
Code :

class Solution {
    
public:
    const int MOD = 1e9 + 7;

    int countPalindromicSubsequences(string s) {
    
        int n = s.size();
        vector<vector<int>> dp(n, vector<int>(n));
        for (int i = 0; i < n; i++) {
    
            dp[i][i] = 1;
        }

        for (int len = 2; len <= n; len++) {
    
            for (int i = 0; i + len <= n; i++) {
    
                int j = i + len - 1;
                if (s[i] == s[j]) {
    
                    int low = i + 1;
                    int high = j - 1;
                    while (low <= high && s[low] != s[i]) {
    
                        low++;
                    }
                    while (high >= low && s[high] != s[j]) {
    
                        high--;
                    }
                    if (low > high) {
    
                        dp[i][j] = (2 + dp[i + 1][j - 1] * 2) % MOD;
                    } else if (low == high) {
    
                        dp[i][j] = (1 + dp[i + 1][j - 1] * 2) % MOD;
                    } else {
    
                        dp[i][j] = (0LL + dp[i + 1][j - 1] * 2 - dp[low + 1][high - 1] + MOD) % MOD;
                    }
                } else {
    
                    dp[i][j] = (0LL + dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1] + MOD) % MOD;
                }
            }
        }

        return dp[0][n - 1];
    }
};

Execution time :96 ms, In all C++ Defeated in submission 95.07% Users of
Memory consumption :35.6 MB, In all C++ Defeated in submission 58.62% Users of
Complexity analysis
Time complexity :O(n2), among n For the string s The length of . The time complexity mainly depends on the number of States to be solved .
Spatial complexity :O(n2), among n For the string s The length of . The spatial complexity mainly depends on the total number of states in the dynamic programming model .
author:LeetCode-Solution

原网站

版权声明
本文为[Sugar_ wolf]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206100950357171.html