动态规划-计数dp-Distinct Subsequences II
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的更多相关文章
- 【leetcode】940. Distinct Subsequences II
题目如下: Given a string S, count the number of distinct, non-empty subsequences of S . Since the result ...
- 【LeetCode】940. Distinct Subsequences II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- [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 ...
- 940. Distinct Subsequences II
Given a string S, count the number of distinct, non-empty subsequences of S . Since the result may b ...
- 动态规划之115 Distinct Subsequences
题目链接:https://leetcode-cn.com/problems/distinct-subsequences/description/ 参考链接:https://www.cnblogs.co ...
- LeetCode 笔记22 Distinct Subsequences 动态规划需要冷静
Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of ...
- Distinct Subsequences ——动态规划
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- uva 10069 Distinct Subsequences(高精度 + DP求解子串个数)
题目连接:10069 - Distinct Subsequences 题目大意:给出两个字符串x (lenth < 10000), z (lenth < 100), 求在x中有多少个z. ...
- LeetCode之“动态规划”:Distinct Subsequences
题目链接 题目要求: Given a string S and a string T, count the number of distinct subsequences of T in S. A s ...
随机推荐
- L2-013 红色警报(25 分)
L2-013 红色警报(25 分)战争中保持各个城市间的连通性非常重要.本题要求你编写一个报警程序,当失去一个城市导致国家被分裂为多个无法连通的区域时,就发出红色警报.注意:若该国本来就不完全连通,是 ...
- linux sort排序命令的高级用法
在linux中,使用sort按行进行排序是很简单的.不过有时,生活总是爱抛给你一个一个的问题.如果使用sort按多个列值排列,同时使用tab作为分隔符,而且对于某些列需要进行逆序排列,这样sort命令 ...
- 菜篮子成血战场!生鲜O2O为何厮杀如此惨烈
衣食住行作为与大众生活直接挂钩的刚需,已经被诸多互联网企业全面渗入,并在彻底颠覆大众原有的生活形态.但其中作为最底层.最基本的"菜篮子"--生鲜市场,似乎仍然没有被互联网元素完全& ...
- [开源福利] Arithmetic Generator
Arithmetic Generator Built with ❤︎ by Simon Ma ✨ A powerful arithmetic generator
- 《Javascript中 == 和 === 的区别》
在js中 ==(相等运算符) 和 === (严格运算符)是两种判断两个变量是否相等的运算符. == :判断是否相等,忽略类型进行值的比较.(存在隐式类型转换的比较) ===:判断是否相等,先判断值是否 ...
- 编写程序实现根据考试成绩将成绩分为A,B,C,D四档。
score = float(input("请输入你的成绩:"))if 90 <= score <= 100: print("你的成绩为A档")eli ...
- 单片机基础——使用GPIO扫描检测按键
1. 准备工作 硬件准备 开发板首先需要准备一个小熊派IoT开发板,并通过USB线与电脑连接. 软件准备 需要安装好Keil - MDK及芯片对应的包,以便编译和下载生成的代码,可参考MDK安装教程 ...
- mysql 学习日记 悲观和乐观锁
理解 悲观锁就是什么事情都是需要小心翼翼,生怕弄错了出大问题, 一般情况下 "增删改" 都是有事务在进行操作的,但是 "查" 是不需要事务操作的, 但是凡事没 ...
- scrapy mid中间件一般处理方法
import user_agent import requests class UA_midd(object): def process_request(self,request,spider): r ...
- php通过单例模式使一个类只能创建一个对象。
单例模式也就是一个类只能创建出一个对象 首先你要知道它的基本思想为:三私一公! 何为三私一公? 1(私).防止用户通过构造方法创建对象,因此私有化构造方法. 2(公).创建一个公共静态函数用来进入 ...