[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 ...
随机推荐
- 每日踩坑 2018-12-25 【Unable to convert MySQL date/time value to System.DateTime】异常
之前的公司一直是用 ORM ,所以底层的事情完全不用在意, 到了这家全是似乎全是 SQL 硬编码 所以也有机会重新去补补以前也没怎么写过的基本 ADO. 不过这两天总有自己写个简易ORM的想法在蠢蠢欲 ...
- 两个或多个线程执行完成之后继续执行之后的步骤,CountDownLatch与CyclicBarrier
开发过程中或多或少会遇到一个方法需要等待两个及以上线程执行结果,如此我们如何处理,这里java提供两个方法CountDownLatch 和CyclicBarrier 方法,以下依次举例说明: Coun ...
- iOS键盘类型最全
一.键盘风格 UIKit框架支持8种风格键盘. typedef enum { UIKeyboardTypeDefault, // 默认键盘:支持所有字符 UIKeyboa ...
- 2013-2014 ACM-ICPC, NEERC, Southern Subregional Contest Problem C. Equivalent Cards 计算几何
Problem C. Equivalent Cards 题目连接: http://www.codeforces.com/gym/100253 Description Jane is playing a ...
- Codeforces Beta Round #14 (Div. 2) A. Letter 水题
A. Letter 题目连接: http://www.codeforces.com/contest/14/problem/A Description A boy Bob likes to draw. ...
- C++ Curiously Recurring Template Prattern(CRTP)例程
简单介绍和例子请参考:C++ 惯用法 CRTP 简介 下面例子为兼顾CRTP和多态的例子. #include <iostream> #include <vector> usin ...
- match_parent和fill_parent的区别(转)
有网友表示对于很多工程中的MATCH_PARENT出现在layout中感到不明白,过去只有FILL_PARENT和WRAP_CONTENT那么match_parent到底是什么类型呢? 其实从Andr ...
- Android SDKVersion 参数列表
http://developer.android.com/guide/topics/manifest/uses-sdk-element.html Platform Version API Level ...
- 【Go命令教程】8. go test
go test 命令用于对 Go 语言编写的程序进行测试.这种测试是以 代码包 为单位的.当然,这还需要测试源码文件的帮助.关于怎样编写并写好 Go 程序测试代码,我们会在本章的第二节加以详述.在这里 ...
- ERROR 2002 (HY000): Can't connect to local MySQL server through socket
在安装好了MySQL之后,使用了新的配置文件后.MySQL服务器能够成功启动,但在登陆的时候出现了ERROR 2002 (HY000): Can't connect to local MySQL se ...