[Leetcode] 730. 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'}.

    思路

    感觉是一道DP的题目,不过自己还是没想到非常好的解决方法,参考了网上的最优解法,确实非常巧妙。

    我们定义dp[len][i][x]表示从索引i开始,长度为len,并且第一个(最后一个)字符是x的子串中,不同回文子序列的个数。那么递推公式为:

    1)如果s[i] != x,那么dp[len][i][x] = dp[len-1][i+1][x],因为此时我们需要忽略掉第一个字符(因为它非x)。

    2)如果s[i+len-1] != x,那么dp[len][i][x] = dp[len-1][i][x],因为此时我们需要忽略掉最后一个字符(因为它非x)。

    3)如果s[i]和s[i+len-1]都为x,那么我们需要计算s[i + 1, i + len - 2]之间的所有不同回文子序列个数。

    我们注意到长度为len的子串的答案只和长度为len-1和len-2的子串的答案有关,而x的取值只有a,b,c,d四种可能,所以我们只需要定义dp[3][i][4],这样就可以将空间复杂度降低到O(N)。而时间复杂度为O(N^2)。

    代码

    class Solution {
    public:
        int countPalindromicSubsequences(string S) {
            int md = 1000000007;
            int n = S.size();
            int dp[3][n][4];
            for (int len = 1; len <= n; ++len) {
                for (int i = 0; i + len <= n; ++i) {
                    for (int x = 0; x < 4; ++x)  {
                        int &ans = dp[2][i][x];
                        ans = 0;
                        int j = i + len - 1;
                        char c = 'a' + x;
                        if (len == 1) {
                            ans = S[i] == c;
                        }
                        else {
                            if (S[i] != c) {
                                ans = dp[1][i+1][x];
                            }
                            else if (S[j] != c) {
                                ans = dp[1][i][x];
                            }
                            else {
                                ans = 2;
                                if (len > 2) {
                                    for (int y = 0; y < 4; ++y) {
                                        ans += dp[0][i+1][y];
                                        ans %= md;
                                    }
                                }
                            }
                        }
                    }
                }
                for (int i = 0; i < 2; ++i) {
                    for (int j = 0; j < n; ++j) {
                        for (int x = 0; x < 4; ++x) {
                            dp[i][j][x] = dp[i+1][j][x];
                        }
                    }
                }
            }
            int ret = 0;
            for (int x = 0; x < 4; ++x) {
                ret = (ret + dp[2][0][x]) % md;
            }
            return ret;
        }
    };

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值