动态规划-计数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 ...
随机推荐
- pymongo bugfix后记
有网友反馈py-mongo-sync同步异常,检查发现curosr[0]取查询结果第一个文档时报错"no such item for Cursor instance". 这里的逻辑 ...
- sycCMS PHP V1.0---呵呵呵呵呵
闲的无聊,随便找了份代码看了看. //search.php 第17行 第49行 ...... $keyword=SafeRequest("keyword","post&q ...
- 从谷歌到脸书:为何巨头纷纷“钟情于”VR相机?
VR的火爆,自然无需多言.而基于VR这一个概念,已经在多个相关行业不断衍生出新的产品.服务或内容.VR眼镜.VR头盔.VR相机.VR游戏.VR影视.VR应用--但VR产业的发展并不是齐头并进,而是出现 ...
- JavaScript中的innerHTML属性的使用
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.html * 作者:常轩 * 微信公众号:Worldh ...
- Java 在PDF中添加表格
本文将介绍通过Java编程在PDF文档中添加表格的方法.添加表格时,可设置表格边框.单元格对齐方式.单元格背景色.单元格合并.插入图片.设置行高.列宽.字体.字号等. 使用工具:Free Spire. ...
- p标签内不能嵌套块级标签
今天突然发现一个问题,那就是p标签内不能嵌套块级标签 例如: <p><p></p></p> 会被浏览器解析成 我又把 div 嵌套在里面,发现还是这样 ...
- 提取.bank音频包。 Extract .bank audio files
转载请注明出处! 首先我们需要提取的文件是 .bank 的音频文件包,里面包含很条音频. 这是我们会用到工具 step1: 运行 quickbms.exe, 它会自动打开选择文件窗口,我们直接选择下 ...
- Deepin环境下启动Pycharm没有启动图标解决办法
小伙伴们在deepin下运行pycharm时,是不是需要通过sh文件启动? 下面告诉大家如何将pycharm图标放在桌面上: 1.在桌面打开终端,输入命令: sudo gedit /usr/share ...
- 文本编辑器 - Sublime Text 3 换行无法自动缩进的解决方法
一.换行无法自动缩进的问题,如图: 稍微查了一下网上的办法,是把汉化文件删除,但是会造成菜单栏混乱,简直无法忍受... 那么这里介绍的是另一种解决办法.在用户的热键配置文件(preferences-k ...
- freecplus框架,Linux平台下C/C++程序员提高开发效率的利器
目录 一.freecplus框架简介 二.freecplus开源许可协议 三.freecplus框架内容 字符串操作 2.xml解析 3.日期时间 4.目录操作 5.文件操作 6.日志文件 7.参数文 ...