https://leetcode.com/problems/distinct-subsequences/description/

求串s中有几个字串t?

开始看成了s和t有几个公共字串,想来也是dp。题目中明显s和t不是对称的。

对于s中最后字符c,如果和t中最后一个不同,那么去掉这个c是对结果无影响的。

如果相同,我们有两种选择:用它,不用它。

于是就是:

if s[i]==t[j]:    dp[i][j]=dp[i-1][j-1]+dp[i-1][j]else:    dp[i][j] = dp[i - 1][j]

code:
class Solution:def update(self,i,j):if i<0 or j<0:return 0def numDistinct(self, s, t):""":type s: str:type t: str:rtype: int"""sl=len(s)tl=len(t)if sl*tl==0:return 0dp=[[0]*tl for _ in range(sl)]if s[0]==t[0]:dp[0][0]=1for i in range(1,sl):dp[i][0]=dp[i-1][0]if s[i]==t[0]:dp[i][0]+=1for i in range(1,sl):for j in range(1,tl):if s[i]==t[j]:dp[i][j]=dp[i-1][j-1]+dp[i-1][j]else:dp[i][j] = dp[i - 1][j]# print(dp)return dp[-1][-1]
s=Solution()
print(s.numDistinct("babgbag", "bag"))
# print(s.numDistinct("rabbbit", "rabbit"))

转载于:https://www.cnblogs.com/waldenlake/p/9678522.html

lc 115. Distinct Subsequences相关推荐

  1. 【重点!DP】LeetCode 115. Distinct Subsequences

    LeetCode 115. Distinct Subsequences Solution1: 不会做.. 参考网址:https://www.youtube.com/watch?v=mPqqXh8XvW ...

  2. LeetCode:115. Distinct Subsequences

    题目 Given a string S and a string T, count the number of distinct subsequences of S which equals T. A ...

  3. leetcode 115. Distinct Subsequences Hard | 115. 不同的子序列(动态规划)

    题目 https://leetcode.com/problems/distinct-subsequences/ 题解 方法1:递归(超时) 这种解法比较容易理解,时间复杂度没算出来,但肯定不是 O(m ...

  4. 【Leetcode】115. Distinct Subsequences

    又是一道DP的题目,个人感觉DP的题目比较难,主要因为:(1)DP的难点是寻找子问题,如果找到很好的子问题,那么就可以瞬间搞定.(2)通常也会带有一点backtracking的思想,有时候总是优先想到 ...

  5. Distinct Subsequences@LeetCode

    Distinct Subsequences 动态规划题.先用二维动态规划的思路解释下:设match是动态规划表,其中match[i][j]表示S.substring(0, i)对T.substring ...

  6. [LeetCode]Distinct Subsequences,解题报告

    题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...

  7. 【leetcode】940. Distinct Subsequences II

    题目如下: Given a string S, count the number of distinct, non-empty subsequences of S . Since the result ...

  8. UVA 10069 Distinct Subsequences(DP)

    考虑两个字符串,我们用dp[i][j]表示字串第到i个和字符串到第j个的总数,由于字串必须连续 因此dp[i][j]能够有dp[i][j-1]和dp[i-1][j-1]递推而来,而不能由dp[i-1] ...

  9. Distinct Subsequences

    https://leetcode.com/problems/distinct-subsequences/discuss/37327/Easy-to-understand-DP-in-Java 如果S[ ...

最新文章

  1. Hyper-V Server 2008系统管理实战
  2. 笔记-项目管理基础知识-项目组织结构
  3. 用Javascript实现面向对象编程(封装,抽象,继承,多态)
  4. C语言求解100的带分数形式的代码
  5. java 获取所有线程,Java 实例 - 获取所有线程
  6. Celo计划推出一种与欧元挂钩的新稳定币
  7. bzoj 1406: [AHOI2007]密码箱
  8. php yii composer,yii2怎么用composer生成一个应用?
  9. Computing Platform------系统平台及其系列
  10. ajaxSubmit问题求解
  11. 可视化工具netron的使用
  12. 为什么我的儿子不沉迷游戏?一位游戏策划家长的分享
  13. 51单片机入门之点亮发光二极管
  14. No matter what,just do not give up。
  15. 关于meta标签中的http-equiv属性使用介绍
  16. BGP Aggregation – Suppress Map
  17. PostgreSQL的学习心得和知识总结(五十三)|语法级自上而下完美实现MySQL数据库的 insert set 的实现方案
  18. 图片上的遮挡物怎么去除?
  19. python决策树的应用_决策树应用(一)
  20. 精妙绝伦!阿里资深架构师撰写这份:并发编程,可谓“独具匠心”

热门文章

  1. 截取屏幕并保存为BMP文件
  2. 入侵检测系统的性能的辨别(3)
  3. ACM MM2021 HANet:从局部到整体的检索!阿里提出用于视频文本检索的分层对齐网络HANet!代码已开源!...
  4. 视频增强之“动态范围扩展”HDR技术漫谈
  5. MediaPipe: Google Research 开源的跨平台多媒体机器学习模型应用框架
  6. 日本东北大学改进单阶段人脸检测—兼具速度与精度优势
  7. 43岁被裁员,200万年薪泡汤:这4件事你要尽早明白
  8. 计算机视觉中的Transformer的最新进展!
  9. 蒙特利尔大学助理教授唐建《图表示学习:算法与应用》研究进展
  10. 从零开始用Python搭建超级简单的点击率预估模型