Distinct Subsequences (dp)
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相同的有多少个
空串是任意字符串的子串。
代码:
class Solution {
public:
int numDistinct(string S, string T) {
int row=S.size()+;
int col=T.size()+;
vector<int> temp(col,);
vector<vector<int>> dp(row,temp);
dp[][]=;
for(int i=;i<row;++i) dp[i][]=;
for(int i=;i<row;++i){
for(int j=;j<=i&&j<col;++j){
if(S[i-]==T[j-]){
dp[i][j]=dp[i-][j-]+dp[i-][j];
}else{
dp[i][j]=dp[i-][j];
}
}
}
return dp[row-][col-];
}
};
Distinct Subsequences (dp)的更多相关文章
- uva 10069 Distinct Subsequences(高精度 + DP求解子串个数)
题目连接:10069 - Distinct Subsequences 题目大意:给出两个字符串x (lenth < 10000), z (lenth < 100), 求在x中有多少个z. ...
- 115. Distinct Subsequences (String; DP)
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- uva 10069 Distinct Subsequences 【dp+大数】
题目:uva 10069 Distinct Subsequences 题意:给出一个子串 x 和母串 s .求子串在母串中的不同序列的个数? 分析:定义dp[i][j]:x 的前 i 个字母在 s 的 ...
- UVa 10069 Distinct Subsequences(大数 DP)
题意 求母串中子串出现的次数(长度不超过1后面100个0 显然要用大数了) 令a为子串 b为母串 d[i][j]表示子串前i个字母在母串前j个字母中出现的次数 当a[i]==b[j]&am ...
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- LeetCode(115) Distinct Subsequences
题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...
- [Leetcode][JAVA] Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- Distinct Subsequences Leetcode
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 【LeetCode OJ】Distinct Subsequences
Problem Link: http://oj.leetcode.com/problems/distinct-subsequences/ A classic problem using Dynamic ...
随机推荐
- phpstorm中快速添加函数注释
Preferences 或 command+,快捷键 Live Templates - PHP 下方 - 新建模板 ,Abbreviation 命名随便写,点击Edit Variables配置变量信息 ...
- Javaweb学习笔记2—Tomcat和http协议
今天来讲javaweb的第二个阶段学习. 老规矩,首先先用一张思维导图来展现今天的博客内容. ps:我的思维是用的xMind画的,如果你对我的思维导图感兴趣并且想看到你们跟详细的备注信息,请点击下 ...
- How `new’ operator works ?
这是2013年写的一篇旧文,放在gegahost.net上面 http://raison.gegahost.net/?p=15 February 15, 2013 How `new’ operator ...
- ABAP和XML数据格式互相转换的两种方式
ABAP和XML数据格式互相转换是广大开发人员经常遇到的需求.本文介绍两种方式. 1. ABAP提供了一个工具类cl_proxy_xml_transform,通过它的两个方法abap_to_xml_x ...
- 1-3 编程基础 makefile工程管理
GNU make Linux程序员必须学会使用GNU make来构建和管理自己的软件工程.GNU的make能够使整个工程的编译.链接只需要一个命令就可以完成. makefile make在执行时,需要 ...
- b继承a的函数
var cls={ my:, init:function() { alert(this.my.a); }};window.onload=function(){ cls.init();} 调用cls.i ...
- 小程序01 微信小程序介绍和开发准备
前言 火爆的微信小程序:跳一跳.摩拜单车.美柚.大众点评.滴滴出行 背景 为什么会有小程序? 微信最早推出公众号和服务号,公众号和服务号所传播的网页经常出现加载缓慢甚至页面空白的情况. 微信小程序的诞 ...
- 【亲测可行】Dev c++调试、运行报错解决方法总结
一.编译后 0错误 0警告,但是开始出现‘‘停止运行’’或者进行输入时出现‘‘停止运行’’ 可能的原因: 结构体指针为空,但调用了其成员. 有些scanf语句中忘记添加取址符. 无法跳出递归. 二. ...
- UVa-340-猜数字
#include <stdio.h> char ans[1000],gus[1000]; int num[10]; int main() { int n,cnt=1; while (sca ...
- tcpdump抓包指令使用示例
tcpdump是一个用于截取网络分组,并输出分组内容的工具. tcpdump凭借强大的功能和灵活的截取策略,使其成为类UNIX系统下用于网络分析和问题排查的首选工具.tcpdump提供了源代码,公开了 ...