zoukankan      html  css  js  c++  java
  • LeetCode 730. Count Different Palindromic Subsequences

    原题链接在这里:https://leetcode.com/problems/count-different-palindromic-subsequences/

    题目:

    Given a string S, find the number of different non-empty palindromic subsequences in S, and return that number modulo 10^9 + 7.

    A subsequence of a string S is obtained by deleting 0 or more characters from S.

    A sequence is palindromic if it is equal to the sequence reversed.

    Two sequences A_1, A_2, ... and B_1, B_2, ... are different if there is some i for which A_i != B_i.

    Example 1:

    Input: 
    S = 'bccb'
    Output: 6
    Explanation: 
    The 6 different non-empty palindromic subsequences are 'b', 'c', 'bb', 'cc', 'bcb', 'bccb'.
    Note that 'bcb' is counted only once, even though it occurs twice.

    Example 2:

    Input: 
    S = 'abcdabcdabcdabcdabcdabcdabcdabcddcbadcbadcbadcbadcbadcbadcbadcba'
    Output: 104860361
    Explanation: 
    There are 3104860382 different non-empty palindromic subsequences, which is 104860361 modulo 10^9 + 7. 

    Note:

    • The length of S will be in the range [1, 1000].
    • Each character S[i] will be in the set {'a', 'b', 'c', 'd'}.

    题解:

    Let dp[i][j] denotes the count of palindromic subsequences of S, i - j inclusive.

    If S[i] != S[j], dp[i][j] = dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1]. Since dp[i + 1][j - 1] is accumlated twice here, it needs to be decreased.

    Else, dp[i][j] = 2 * dp[i + 1][j - 1] + 2. e.g. "bccb", for "cc" it has 2 palindromic subsequences "c" and "cc". When adding outer "b", it could be "bcb" and "bccb". That is why it times 2. And it could have "b" and "bb", that is why it adds 2.

    It has 2 exceptions, 1 is when middle part has one occurance of "b", like "bcbcb", since "b" is already added when checking "cbc", then outer "b" is duplicate, then dp[i][j] = 2 * dp[i][j] + 1.

    The other exception is middle part has more than one occurance of "b". like "bbccbb". Since middle part "bccb" already has "bb", "b", "bcb" and "bccb" added, we don't want to add duplicate.

    then dp[i][j] = 2 * dp[i + 1][j - 1] - dp[l + 1][r - 1]. "bb" and "b" are + 2. "bcb" and "bccb" are "dp[l + 1][r - 1]".

    l is most left occurance after i of S[i], r is most right occurance before j of S[j].

    Time Complexity: O(n ^ 3). n = S.length().

    Space: O(n ^ 2).

    AC Java:

     1 class Solution {
     2     public int countPalindromicSubsequences(String S) {
     3         if(S == null || S.length() == 0){
     4             return 0;
     5         }
     6         
     7         int mod = 1000000007;
     8         int n = S.length();
     9         
    10         char [] chs = S.toCharArray();
    11         int [][] dp = new int[n][n];
    12         
    13         for(int i = 0; i < n; i++){
    14             dp[i][i] = 1;
    15         }
    16         
    17         for(int d = 1; d < n; d++){
    18             for(int i = 0; i < n - d; i++){
    19                 int j = i + d;
    20                 if(chs[i] != chs[j]){
    21                     dp[i][j] = dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1];
    22                 }else{
    23                     int l = i + 1;
    24                     int r = j - 1;
    25                     while(l <= r && chs[l] != chs[i]){
    26                         l++;
    27                     }
    28                     
    29                     while(l <= r && chs[r] != chs[j]){
    30                         r--;
    31                     }
    32                     
    33                     if(l > r){
    34                         dp[i][j] = 2 * dp[i + 1][j - 1] + 2;
    35                     }else if(l == r){
    36                         dp[i][j] = 2 * dp[i + 1][j - 1] + 1;
    37                     }else{
    38                         dp[i][j] = 2 * dp[i + 1][j - 1] - dp[l + 1][r - 1];
    39                     }
    40                 }
    41                 
    42                 dp[i][j] = dp[i][j] < 0 ? dp[i][j] + mod : dp[i][j] % mod;
    43             }
    44         }
    45         
    46         return dp[0][n - 1];
    47     }
    48 }

    类似Longest Palindromic Subsequence.

  • 相关阅读:
    挺好用的SQLSERVER数据库自动备份工具SQLBackupAndFTP(功能全面)
    SQLSERVER中的鬼影索引
    SQLSERVER NULL和空字符串的区别 使用NULL是否节省空间
    SQLSERVER中NULL位图的作用
    SQLSERVER到底能识别多少个逻辑CPU?
    不正常关机引起的数据库置疑
    如何在大型的并且有表分区的数据库中进行DBCC CHECKDB操作
    索引视图是否物理存储在数据库中以及使用索引视图的一些见解
    Oracle非重要文件恢复,redo、暂时文件、索引文件、password文件
    最大匹配、最小顶点覆盖、最大独立集、最小路径覆盖(转)
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12531495.html
Copyright © 2011-2022 走看看