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.

  • 相关阅读:
    访问 IIS 元数据库失败 的解决方法
    【转】IIS网站访问需要输入用户名和密码
    jQuery以Post方式发送请求并获取返回的文件供下载!
    moss2007安装
    货币加逗号 javascript/js
    vs2005 sp1 安装包签名错误解决办法 fid=73863 error 1718
    xpath ignore case query/xpath 不区分大小写
    moss2007 webpart custom property sample
    字符转拼音 javascript pinyin
    moss2007 webpart custom property sample (2) [a simple one, inherit from .net framework webpart]
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12531495.html
Copyright © 2011-2022 走看看