动态规划-计数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 ...
随机推荐
- Kubelet
Kubelet 相关博客 Kubelet组件深度解析 Kubelet组件解析 Kubelet运行机制分析 Kubelet与apiserver通信 ___ Kubelet组件运行在Node节点上,维持运 ...
- CPU踩点图
CPU占比探测用js来检查当前系统cpu的占用比例,通过 setTimeout 的方式探测 CPU 的大小,这样可以实现网页游戏中动画等耗时操作的自动调节.这个原理是很多人都知道的,就是用JS来踩点. ...
- flutter 入门(Mac)
背景 近一年时间都在用 React Native 开发公司的 APP,水平基本上可以说是登堂入室了.前一段时间兴起一阵 Flutter 热,后端的同事都有推荐,今年 Google 大会又推出 flut ...
- 创建git密钥
前言 git使用https协议,每次pull,push都要输入密码,使用git协议,使用ssh秘钥,可以省去每次输密码 大概需要三个步骤: 一.本地生成密钥对: 二.设置github上的公钥: 三.修 ...
- 图解Python网络编程
返回目录 本篇索引 (1)基本原理 (2)socket模块 (3)select模块 (4)asyncore模块 (5)asynchat模块 (6)socketserver模块 (1)基本原理 本篇指的 ...
- 37个JavaScript基本面试问题和解答
1.使用typeof bar ==="object"来确定bar是否是一个对象时有什么潜在的缺陷?这个陷阱如何避免? 尽管typeof bar ==="object&qu ...
- intel硬件视频加速介绍
目录 硬件视频加速技术 intel 硬件加速技术 intel 的开源媒体栈 VA-API 安装 样例 Intel Quick Sync(QSV) API支持情况 vaapi/mfx比较 安装 样例 硬 ...
- 关于在elasticSearch中使用聚合查询后只显示10个bucket的问题
先看下面es查询语句 { "size": 0, "aggs" : { "all_articleId" : { "terms&quo ...
- sf-git机制
为什么要专门写一篇关于sf科技公司的GIT管理机制呢?因为本周经历了两天的学习和考试,刚开始没在意,因为之前公司也用的GIT,所以没怎么看视频,就看了文档,练习考试时候才发现并非以前的那种git流程, ...
- Spring Boot 自动装配流程
Spring Boot 自动装配流程 本文以 mybatis-spring-boot-starter 为例简单分析 Spring Boot 的自动装配流程. Spring Boot 发现自动配置类 这 ...