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 ...
 
随机推荐
- python学习笔记4_数据清洗与准备
			
一.处理缺失值 pandas使用浮点值NaN(Not a Number)来显示缺失值,并将缺失值称为NA(not available(不可用)). NA常用处理方法: dropna:根据每个标签的值是 ...
 - HtmlHelper1
			
<div> @using(Html.BeginForm("Test","Default")) { 4 @Html.TextBox("nam ...
 - VC文件操作
			
VC文件操作 重命名文件: 注意: 该操作对文件夹一样有效! CFileFind Finder; CString sOldPath = _T("D:\\tt.txt"); CStr ...
 - virtualbox 启动虚拟机提示Cannot load R0 module
			
Cannot load R0 module C:\Program Files\Oracle\VirtualBox/VBoxDDR0.r0: SUPR3LoadModule: supLoadModule ...
 - 初识css3 3d动画效果
			
(先看我博客右上角的3d盒子动画效果,目前没做兼容处理,最好最新的chrome看)无意间看到网上css3写的3d动画效果,实在炫酷,以前理解为需要js去计算去写,没想到css直接可以实现.于是开始研究 ...
 - Ionic 日期时间插件
			
1.插件安装 日期插件 时间插件 备注: 具体 查看 https://github.com/rajeshwarpatlolla/ionic-datepicker https://github.c ...
 - 【一坨理论AC的题】Orz sxy大佬
			
1.UVA10891 Game of Sum 2.LA4254 Processor . 3.UVA10905 Children's Game 4.UVA11389 The Bus Driver Pro ...
 - (转载)My97 datepicker使用指南
			
这里先显示大家都可以看到的My97DatePicker的用法: WdatePicker日历控件使用方法(http://www.cnblogs.com/yuhanzhong/archive/2011/0 ...
 - SQLSERVER 数据库管理员的专用连接DAC
			
DAC:Dedicated Admin Connection 当SQL Server因系统资源不足,或其它异常导致无法建立数据库连接时, 可以使用系统预留的DAC连接到数据库,进行一些问题诊断和故障排 ...
 - PhoneInfoga---用于电话号码的信息收集和OSINT侦察工具
			
PhoneInfoga 是仅使用免费资源扫描电话号码的最先进工具之一. 目标是首先在任何国际电话号码上收集标准信息, 如国家,地区,运营商和线路类型,并且准确性非常高. 然后在搜索引擎上搜索足迹以尝试 ...