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 ...
随机推荐
- Android(java)学习笔记157:开源框架的文件上传(只能使用Post)
1.文件上传给服务器,服务器端必然要写代码进行支持,如下: 我们新建一个FileUpload.jsp的动态网页,同时我们上传文件只能使用post方式(不可能将上传数据拼凑在url路径下),上传数据Ap ...
- CSS继承inherit | elementUI NavMenu vertical竖版 加 A标记 外联 不能继承上层color,需要手写下color:inherit;
<li data-v-576b9cf5="" role="menuitem" tabindex="0" class="el- ...
- Mac 下用homebrew安装配置MongoDB
---恢复内容开始--- 1.首先安装homebrew,已有就跳过 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent. ...
- fio测试nvme性能
#cat /sys/block/nvme0n1/queue/scheduler none #cat /sys/block/sda/queue/scheduler noop deadline [cfq] ...
- luogu P1455 搭配购买
题目描述 明天就是母亲节了,电脑组的小朋友们在忙碌的课业之余挖空心思想着该送什么礼物来表达自己的心意呢?听说在某个网站上有卖云朵的,小朋友们决定一同前往去看看这种神奇的商品,这个店里有n朵云,云朵已经 ...
- Linux 系统内存分析
1. 内存基本介绍 1.计算机基本结构: 电脑之父--冯·诺伊曼提出了计算机的五大部件:输入设备.输出设备.存储器.运算器和控制器 如图: 输入设备:键盘鼠标等 CPU:是计算机的运算核心和控制核心, ...
- django扩展User模型(model),profile
from django.contrib.auth.models import User # Create your models here. class Profile(models.Model): ...
- babel实践
现在的主流浏览器还没有完全兼容ES6的语法,如ie11就不支持箭头函数. 使用过es6的人都知道,es6更加简洁和强大,可是使用es6写出来的代码并不能得到所有主流js引擎的支持,针对这一点,一个解决 ...
- Django reverse函数
1.总urls.py内容如下: from django.contrib import admin from django.urls import path from django.conf.urls ...
- 如何在Python中显式释放内存?
根据Python官方文档,您可以强制垃圾收集器释放未引用的内存gc.collect().例: import gc gc.collect() 所属网站分类: python高级 > 综合&其 ...