[leetcode]Distinct Subsequences @ Python
原题地址:https://oj.leetcode.com/problems/distinct-subsequences/
题意:
Given a string S and a string T, count the number of distinct subsequences of T in S.
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ACE"
is a subsequence of "ABCDE"
while "AEC"
is not).
Here is an example:
S = "rabbbit"
, T = "rabbit"
Return 3
.
解题思路:这道题使用动态规划来解决。题的意思是:S的所有子串中,有多少子串是T。下面来看看状态转移方程。dp[i][j]表示S[0...i-1]中有多少子串是T[0...j-1]。
当S[i-1]=T[j-1]时:dp[i][j]=dp[i-1][j-1]+dp[i-1][j];S[0...i-1]中有多少子串是T[0...j-1]包含:{S[0...i-2]中有多少子串是T[0...j-2]}+{S[0...i-2]中有多少子串是T[0...j-1]}
当S[i-1]!=T[j-1]时:dp[i][j]=dp[i-1][j-1]
那么初始化状态如何确定呢:dp[i][0]=1;S[0...i-1]只有一个子串是空串。
代码:
- class Solution:
- # @return an integer
- # @dp
- # dp[i][j] means how many first j of T is sub of first i of S.
- def numDistinct(self, S, T):
- dp = [[0 for i in range(len(T)+1)] for j in range(len(S)+1)]
- for j in range(len(S)+1):
- dp[j][0] = 1
- for i in range(1, len(S)+1):
- for j in range(1, min(i+1, len(T)+1)):
- if S[i-1] == T[j-1]:
- dp[i][j] = dp[i-1][j] + dp[i-1][j-1]
- else:
- dp[i][j] = dp[i-1][j]
- return dp[len(S)][len(T)]
[leetcode]Distinct Subsequences @ Python的更多相关文章
- 子序列 sub sequence问题,例:最长公共子序列,[LeetCode] Distinct Subsequences(求子序列个数)
引言 子序列和子字符串或者连续子集的不同之处在于,子序列不需要是原序列上连续的值. 对于子序列的题目,大多数需要用到DP的思想,因此,状态转移是关键. 这里摘录两个常见子序列问题及其解法. 例题1, ...
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- Leetcode Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- [LeetCode] Distinct Subsequences 解题思路
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- LeetCode: Distinct Subsequences [115]
[称号] Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequ ...
- LeetCode: Distinct Subsequences 解题报告
Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of ...
- [LeetCode] Distinct Subsequences [29]
题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...
- [Leetcode] distinct subsequences 不同子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 【LeetCode】940. Distinct Subsequences II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
随机推荐
- [代码审计]yxcms从伪xss到getshell
0x00 前言 这篇文章首发于圈子,这里作为记录一下. 整个利用链构造下来是比较有趣的,但实际渗透中遇到的几率比较少. 此次审的是yxcms 1.4.6版本,应该是最后一个版本了吧? 0x01 从任意 ...
- 【FFT&NTT 总结】
$FFT$总结 (因为还不会啊,,都没做过什么题,所以一边学一边打咯.. 1.主要是用来加速卷积形式的求和吧? $F*G(n)=F[i] × G[n-i]$ 平时是$O(n^2)$的,FFT可以$O( ...
- Beta冲刺准备
过去存在的问题: 界面不够美观 推荐不够人性化 代码不够符合开闭原则 我们已经做了哪些调整/改进: 本来想引入springAndroid,但看了下google的官方文档,不建议引入第三方框架:代码重构 ...
- [USACO07JAN]Balanced Lineup
OJ题号:洛谷2880 思路1: 线段树维护区间最大最小值. #include<cstdio> #include<cctype> #include<utility> ...
- Ural 2037. Richness of binary words 打表找规律 构造
2037. Richness of binary words 题目连接: http://acm.timus.ru/problem.aspx?space=1&num=2037 Descripti ...
- UVALive 6910 Cutting Tree 并查集
Cutting Tree 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8& ...
- Codeforces Round #511 (Div. 2)
Codeforces Round #511 (Div. 2) #include <bits/stdc++.h> using namespace std; int n; int main() ...
- Linux下查看哪些IP登陆过系统/var/log/wtmp
last -f /var/log/wtmp
- Linux下Shell函数返回值实现种类
shell在执行的时候是顺序执行的,也不存在什么多线程什么的. 一下是实现种类: 1.全局 g_result="" function testFunc() { g_result=' ...
- 使用CefSharp在.Net程序中嵌入Chrome浏览器(九)——性能问题
在使用CEF的过程中,我发现了一个现象:WPF版的CEF比Chrome性能要差:一些有动画的地方会掉帧(例如,CSS动画,全屏图片拖动等),视频播放的效果也没有Chrome流畅. 查了一下相关资料,发 ...