当前位置:网站首页>LeetCode: Distinct Subsequences [115]
LeetCode: Distinct Subsequences [115]
2022-07-05 20:47:00 【全栈程序员站长】
大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。
【称号】
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,通过删除S中的若干个字符能够得到T,则T称为S的子序列。问有多少种删法能够得到T
【思路】
DP问题。 定义A[i][j] (i>j)表示S[1…i]转化到T[1…j]的方式数(操作类型仅仅有一种。那就是从S中删除若干字符)。 转换方程例如以下: 假设S[i]==T[j], A[i][j]=A[i-1][j-1]+A[i-1][j]; 假设S[i]!=T[j], A[i][j]=A[i-1][j]; 初始化矩阵 起始点A[0][0]=1; 第一行A[0][i]=0; 第一列A[i][j]=1;
【代码】
class Solution {
public:
int numDistinct(string S, string T) {
if(S.length()==0 || S.length()==0)return 0;
if(S.length()<T.length())return 0; //假设S==T,返回1, 觉得有1种转换方式
vector<vector<int> >matrix(S.length()+1, vector<int>(T.length()+1, 0));
//初始化matrix[0][0]
matrix[0][0]=1;
//初始化对角线
for(int j=1; j<=T.length(); j++)
matrix[0][j]=0;
//初始化第一列
for(int i=1; i<=S.length(); i++)
matrix[i][0]=1;
//考虑其它i>j的情况
for(int i=1; i<=S.length(); i++){
for(int j=1; j<=T.length(); j++){
if(S[i-1]==T[j-1])matrix[i][j]=matrix[i-1][j-1]+matrix[i-1][j];
else matrix[i][j]=matrix[i-1][j];
}
}
return matrix[S.length()][T.length()];
}
};
版权声明:本文博客原创文章。博客,未经同意,不得转载。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/117649.html原文链接:https://javaforall.cn
边栏推荐
- 教你自己训练的pytorch模型转caffe(二)
- The development of research tourism practical education helps the development of cultural tourism industry
- Welcome to the game and win rich bonuses: Code Golf Challenge officially launched
- Duchefa细胞分裂素丨二氢玉米素 (DHZ)说明书
- How to make ERP inventory accounts of chemical enterprises more accurate
- 欢迎来战,赢取丰厚奖金:Code Golf 代码高尔夫挑战赛正式启动
- Duchefa丨低熔点琼脂糖 PPC中英文说明书
- Norgen AAV提取剂盒说明书(含特色)
- Maker education infiltrating the transformation of maker spirit and culture
- Abnova CD81 monoclonal antibody related parameters and Applications
猜你喜欢
Abbkine丨TraKine F-actin染色试剂盒(绿色荧光)方案
Abnova maxpab mouse derived polyclonal antibody solution
基于AVFoundation实现视频录制的两种方式
Duchefa丨D5124 MD5A 培养基中英文说明书
Duchefa s0188 Chinese and English instructions of spectinomycin hydrochloride pentahydrate
解析五育融合之下的steam教育模式
Wanglaoji pharmaceutical's public welfare activity of "caring for the most lovely people under the scorching sun" was launched in Nanjing
Abnova丨E (DIII) (WNV) 重组蛋白 中英文说明书
Abbkine BCA法 蛋白质定量试剂盒说明书
Duchefa p1001 plant agar Chinese and English instructions
随机推荐
10000+ 代码库、3000+ 研发人员大型保险集团的研发效能提升实践
研学旅游实践教育的开展助力文旅产业发展
Go file path operation
2.<tag-哈希表, 字符串>补充: 剑指 Offer 50. 第一个只出现一次的字符 dbc
How to make ERP inventory accounts of chemical enterprises more accurate
Mathematical analysis_ Notes_ Chapter 9: curve integral and surface integral
Chemical properties and application instructions of prosci Lag3 antibody
When steam education enters personalized information technology courses
How to open an account online for futures? Is it safe?
Duchefa s0188 Chinese and English instructions of spectinomycin hydrochloride pentahydrate
Abbkine丨TraKine F-actin染色试剂盒(绿色荧光)方案
Leetcode (695) - the largest area of an island
AI automatically generates annotation documents from code
Abnova blood total nucleic acid purification kit pre installed relevant instructions
ts 之 类的简介、构造函数和它的this、继承、抽象类、接口
教你自己训练的pytorch模型转caffe(二)
基于AVFoundation实现视频录制的两种方式
常用的视图容器类组件
Abnova丨荧光染料 620-M 链霉亲和素方案
CADD course learning (7) -- Simulation of target and small molecule interaction (semi flexible docking autodock)