美文网首页
(子序列计数dp)1955. 统计特殊子序列的数目

(子序列计数dp)1955. 统计特殊子序列的数目

作者: 来到了没有知识的荒原 | 来源:发表于2021-08-02 12:17 被阅读0次

    1955. 统计特殊子序列的数目

    子序列计数问题,应该是通过dp解决(考虑状态转移方程)

    class Solution {
    public:
        int countSpecialSubsequences(vector<int> &nums) {
            int n = nums.size();
            int f0 = 0, f1 = 0, f2 = 0;
            const int MOD = 1e9 + 7;
            for (int i = 0; i < n; i++) {
                if (nums[i] == 0) f0 = (f0 * 2 % MOD + 1) % MOD;
                if (nums[i] == 1) f1 = (f1 * 2 % MOD + f0) % MOD;
                if (nums[i] == 2) f2 = (f2 * 2 % MOD + f1) % MOD;
            }
            return f2 % MOD;
        }
    };
    

    相关文章

      网友评论

          本文标题:(子序列计数dp)1955. 统计特殊子序列的数目

          本文链接:https://www.haomeiwen.com/subject/dcodvltx.html