LintCode刷题笔记-- 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).
解题思路:
昨天想了很久都没有想明白这一问题,昨天在分析这一问题的时候主要是把需要在二维解决的问题放在了一维上解决了。
如果强行进行降维的话,会导致需要在两个循环中维护一个数组。
所以这一题与先前的最长公共子序列非常类似:在两个向量上分解字符串,dp[i][j]所代表的含义为在S的子串(0,i)与T的子串(0,j)的情况下,两者拥有之间拥有的子序列的数量,对于如果两者遍历到i与j所在的位置的字符相等,则在i-1与j-1的位置上的结果加上1。
S与T两者的子串都不存在这一相同字符的状态即dp[i-1][j-1]下的结果便是先前状态,因为在当前状态下,两者的字符串相同,所以只要在先前状态加上1为当前子序列状态的。
所以有关字符串匹配的动态规划,千万要小心的就是两点,一点是初始化的状态,另外一点是转移时的限制条件和相关的子状态。
发现所有问题很多都是背包问题的变种。
参考代码:
public int numDistinct(String S, String T) {
// write your code here
int[][] dp = new int[S.length()+1][T.length()+1];
dp[0][0] = 1;
for(int i = 1; i<=S.length(); i++){
dp[i][0] = 1;
}
for(int j = 1; j <=T.length(); j++){
dp[0][j] = 0;
}
for(int i = 1; i<=S.length(); i++){
for(int j=1; j<=T.length(); j++){
dp[i][j] = dp[i-1][j];
if(S.charAt(i-1)==T.charAt(j-1)){
dp[i][j] += dp[i-1][j-1];
}
}
}
return dp[S.length()][T.length()];
}
}
LintCode刷题笔记-- Distinct Subsequences的更多相关文章
- lintcode刷题笔记(一)
最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...
- LintCode刷题笔记-- LongestCommonSquence
标签:动态规划 题目描述: Given two strings, find the longest common subsequence (LCS). Your code should return ...
- LintCode刷题笔记-- PaintHouse 1&2
标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...
- LintCode刷题笔记-- Maximum Product Subarray
标签: 动态规划 描述: Find the contiguous subarray within an array (containing at least one number) which has ...
- LintCode刷题笔记-- Maximal Square
标签:动态规划 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing a ...
- LintCode刷题笔记-- Edit distance
标签:动态规划 描述: Given two words word1 and word2, find the minimum number of steps required to convert wo ...
- LintCode刷题笔记-- BackpackIV
标签: 动态规划 描述: Given an integer array nums with all positive numbers and no duplicates, find the numbe ...
- LintCode刷题笔记-- BackpackII
标记: 动态规划 问题描述: Given n items with size Ai, an integer m denotes the size of a backpack. How full you ...
- LintCode刷题笔记-- Update Bits
标签: 位运算 描述: Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set ...
随机推荐
- <每日一题>题目10:求斐波拉契数列
def func(x): m,n = 0,1 i = 0 while i < x: yield m m,n = n,m+n i += 1 fib = [] get_func = func(100 ...
- 19.10.14-Q
小$P$的咕事 总结: 还行,就是$T1$写的慢了,$T2,T3$暴力有点锅 T1 小模拟. 打就是了. 可以小小的手玩一下. (考试的时候某同志人肉对拍了$20min$)=.= 418 ms 360 ...
- java中通过jacob调用dts进行数据导入导出
在一个项目中需要金蝶软件对接,但是业务服务器和财务服务器相隔很远(中间经过好几台服务器,有内网也有外网),从一个内网向另一个内网中传输时,需要外网辅助,因为不让原始数据受污染,使用了DTS数据同步到另 ...
- 实习面试总结(只写了昨天腾讯的面试和拿到offer的一个小公司, 有空再把前面的补上吧)
一个月来面了大大小小的公司有近10个,还是总结一下吧,希望对大家有点用处. 我想说的是,大学四年,如果不会继续读研深造,那么你需要做的不仅仅是疯狂的做项目,或者单独的学算法. 最好的方式就是都了解一点 ...
- vue 路由入门(vue-router)
新建的 js 文件如下: import Vue from 'vue' import VueRouter from 'vue-router' Vue.use(VueRouter) //全局使用该组件 / ...
- JS中的一些函数式编程术语
组合 Composition 组合某种类型(含函数)的两个元素,进而生成一个该类型的新元素: JavaScript 1 2 3 4 5 6 7 let compose = (f ...
- sqlserver数据往别的数据库迁移
背景:sqlserver数据库数据是服务于一个.net项目.后来由于去.net 要将数据前往高斯数据库 或者Oracle. 问题是 当sql Name Age Gender from student ...
- StoryBoard拆分(Storyboard References)
https://www.jianshu.com/p/78dc76204c8e iOS UI篇10- Storyboard(Storyboard Reference) https://www.aliyu ...
- mybatis学习:mybatis的二级缓存
一级缓存: 一级缓存是SqlSession级别的缓存.在操作数据库时需要构造 sqlSession对象,在对象中有一个(内存区域)数据结构(HashMap)用于存储缓存数据.不同的sqlSession ...
- Python中函数的定义必须在调用的前面
# -*- coding:utf-8 -*- Python中函数的定义必须在调用的前面,但是在函数的内部调用一个函数,不用考虑顺序,只要被调用的函数被定义了即可 #标准的先函数定义,后函数调用def ...