2020-02-06 17:01:36

问题描述:

问题求解:

非常经典的计数dp问题,思路就是统计以每个字符为结尾的个数,最后求和即可。

dp[i] = sum of (dp[j]) 0 <= j <= i;可以理解为将最后的一个字符追加到前面的字符串后面。

问题是如何去重。

当我们遇到相同的字符的时候,首先最后一个字符单独最为subseq要删除,因为前面计算过了,其次,只能加到第一次碰到形同字符的位置,因为再前面的在这个重复字符的位置已经计算过了。

    int mod = (int)1e9 + 7;
public int distinctSubseqII(String S) {
int n = S.length();
int[] dp = new int[n];
Arrays.fill(dp, 1);
for (int i = 0; i < n; i++) {
for (int j = i - 1; j >= 0; j--) {
if (S.charAt(i) != S.charAt(j)) dp[i] = (dp[i] % mod + dp[j] % mod) % mod;
else {
dp[i] -= 1;
dp[i] = (dp[i] % mod + dp[j] % mod) % mod;
break;
}
}
}
int res = 0;
for (int i = 0; i < n; i++) res = (res % mod + dp[i] % mod) % mod;
return res;
}

  

动态规划-计数dp-Distinct Subsequences II的更多相关文章

  1. 【leetcode】940. Distinct Subsequences II

    题目如下: Given a string S, count the number of distinct, non-empty subsequences of S . Since the result ...

  2. 【LeetCode】940. Distinct Subsequences II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  3. [Swift]LeetCode940. 不同的子序列 II | Distinct Subsequences II

    Given a string S, count the number of distinct, non-empty subsequences of S . Since the result may b ...

  4. 940. Distinct Subsequences II

    Given a string S, count the number of distinct, non-empty subsequences of S . Since the result may b ...

  5. 动态规划之115 Distinct Subsequences

    题目链接:https://leetcode-cn.com/problems/distinct-subsequences/description/ 参考链接:https://www.cnblogs.co ...

  6. LeetCode 笔记22 Distinct Subsequences 动态规划需要冷静

    Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of  ...

  7. Distinct Subsequences ——动态规划

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  8. uva 10069 Distinct Subsequences(高精度 + DP求解子串个数)

    题目连接:10069 - Distinct Subsequences 题目大意:给出两个字符串x (lenth < 10000), z (lenth < 100), 求在x中有多少个z. ...

  9. LeetCode之“动态规划”:Distinct Subsequences

    题目链接 题目要求: Given a string S and a string T, count the number of distinct subsequences of T in S. A s ...

随机推荐

  1. Java入门教程六(内置包装类)

    Java 是一种面向对象的编程语言,Java 中的类把方法与数据类型连接在一起,构成了自包含式的处理单元.但在 Java 中不能定义基本类型对象,为了能将基本类型视为对象处理,并能连接相关方法,Jav ...

  2. 《自拍教程36》段位三_Python面向对象类

    函数只能面向过程,来回互相调用后顺序执行, 简单的编码项目,还能应付的过来, 复杂的大型项目,调用多了,就会乱. 如何才能不乱呢,可尝试下, 面向对象类的概念, 将现实世界的事物抽象成对象,将现实世界 ...

  3. d3.js ---画坐标轴

    画坐标轴 //使用d3的svg的axis()方法生成坐标轴 var x_axis = d3.svg.axis().scale(scale_x), y_axis = d3.svg.axis().scal ...

  4. iview中遇到table的坑(已经修改了table的数据,但是界面没有更新)

    https://blog.csdn.net/bigdargon/article/details/89381466 https://blog.csdn.net/qiuyan_f/article/deta ...

  5. [poj1062][最短路]昂贵的聘礼

    (最近总是有想让我的小博客更加充实的冲动,遇见一个不平常的题就想写下来.今天这个题姑且算是同学推荐的好题,很有意思,志之) 题目 题面 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了, ...

  6. MySQL/InnoDB中的事务隔离级别

    SQL标准中的事务四种隔离级别 隔离级别 脏读(Dirty Read) 不可重复读(NonRepeatable Read) 幻读(Phantom Read) 未提交读(Read uncommitted ...

  7. php实现下载功能

    <?php header("Content-type:text/html;charset=utf-8"); $file_name="1.text"; // ...

  8. Python面向对象之反射,双下方法

    一. 反射 反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问.检测和修改它本身状态或行为的一种能力(自省).这一概念的提出很快引发了计算机科学领域关于应用反射性的研究.它首先被程序 ...

  9. 【猫狗数据集】使用预训练的resnet18模型

    数据集下载地址: 链接:https://pan.baidu.com/s/1l1AnBgkAAEhh0vI5_loWKw提取码:2xq4 创建数据集:https://www.cnblogs.com/xi ...

  10. JVM进阶:JVM的监控利器

    每次聊起性能测试,最后的终极话题就是怎么做优化.其实在Java的复杂项目中都会有内存不足问题.内存泄露问题.线程死锁问题.CPU问题.这些问题在小压力的情况下有可能并不明显,很容易被忽视.但是真正到了 ...