标签:动态规划

题目描述:

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的更多相关文章

  1. lintcode刷题笔记(一)

    最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...

  2. LintCode刷题笔记-- LongestCommonSquence

    标签:动态规划 题目描述: Given two strings, find the longest common subsequence (LCS). Your code should return ...

  3. LintCode刷题笔记-- PaintHouse 1&2

    标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...

  4. LintCode刷题笔记-- Maximum Product Subarray

    标签: 动态规划 描述: Find the contiguous subarray within an array (containing at least one number) which has ...

  5. LintCode刷题笔记-- Maximal Square

    标签:动态规划 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing a ...

  6. LintCode刷题笔记-- Edit distance

    标签:动态规划 描述: Given two words word1 and word2, find the minimum number of steps required to convert wo ...

  7. LintCode刷题笔记-- BackpackIV

    标签: 动态规划 描述: Given an integer array nums with all positive numbers and no duplicates, find the numbe ...

  8. LintCode刷题笔记-- BackpackII

    标记: 动态规划 问题描述: Given n items with size Ai, an integer m denotes the size of a backpack. How full you ...

  9. LintCode刷题笔记-- Update Bits

    标签: 位运算 描述: Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set ...

随机推荐

  1. tiger-complier 问题记录 类型检查

    1 注意区分 Ty_xxx 和 a_ 的区别 A 开头是对应的是原程序中的一个实体 Ty _ 开头则是类型检查中的一个抽象类 代表了一类实体 比如 A_recordTy 是原程序中的 type per ...

  2. 初学C#的简单编程题合集(更新)

    一 编写一个控制台应用程序,要求完成下列功能. 1)   接收一个整数 n. 2)   如果接收的值 n 为正数,输出 1 到 n 间的全部整数. 3)   如果接收的值为负值,用 break 或者 ...

  3. Leetcode152. Maximum Product Subarray乘积的最大子序列

    给定一个整数数组 nums ,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数). 示例 1: 输入: [2,3,-2,4] 输出: 6 解释: 子数组 [2,3] 有最大乘积 6. 示例 2 ...

  4. 如何通过EditPlus远程连接Linux

    1. File - FTP - FTP Settings 2. Add 3. 填写Linux的ip地址及用户名和密码 4. OK

  5. 为互联网业务而生:阿里云全球首发云Cassandra服务!

    引言:十年沉淀.全球宽表排名第一.阿里云首发云Cassandra服务 ApsaraDB for Cassandra是基于开源Apache Cassandra,融合阿里云数据库DBaaS能力的分布式No ...

  6. Flannel部署

    目录 Flannel CNI集成 配置Docker使用Flannel 1.为Flannel生成证书 [root@linux-node1 ~]# cd /usr/local/src/ssl/ [root ...

  7. Ubuntn16.04+OpenCV3.1+CUDA8.0+cudnn5.1+caffe配置及问题集锦

    ubuntn16.04 Caffe安装步骤记录(超详尽) 一开始安装好ubuntn16.04后,先安装的opencv3.1,再自己安装的390驱动,cuda8.0和cudnn,之后配置caffe一直不 ...

  8. php循环语句(一)

    PHP 循环语句 什么是循环语句? 在不少实际问题中有许多具有规律性的重复操作,因此在程序中就需要重复执行某些语句.一组被重复执行的语句称之为循环体,能否继续重复,决定循环的终止条件.循环结构是在一定 ...

  9. 使用C#反射实现用户控件调用父页面方法

    using System.Reflection; MethodInfo mi = this.Page.GetType().GetMethod("GetUserName"); //该 ...

  10. Vuejs实战项目五:数据列表

    1.在EasyMock 中添加数据列表模拟接口 请求url:/suyuan/list 请求方式:get 描述:数据列表 mock.js配置: 例: { "code": 2000, ...