当前位置:网站首页>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
边栏推荐
- Norgen AAV提取剂盒说明书(含特色)
- Selenium element information
- How to renew NPDP? Here comes the operation guide!
- 实现浏览页面时校验用户是否已经完成登录的功能
- haas506 2.0开发教程 - 阿里云ota - pac 固件升级(仅支持2.2以上版本)
- Popular science | does poor English affect the NPDP exam?
- 小程序事件绑定
- CCPC 2021 Weihai - G. shinyruo and KFC (combination number, tips)
- Pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
- Chemical properties and application instructions of prosci Lag3 antibody
猜你喜欢

Duchefa丨P1001植物琼脂中英文说明书

1. Strengthen learning basic knowledge points

Chemical properties and application instructions of prosci Lag3 antibody

Frequent MySQL operations cause table locking problems

2022 Beijing eye health products exhibition, eye care products exhibition, China eye Expo held in November

Cutting edge technology for cultivating robot education creativity

Duchefa丨低熔点琼脂糖 PPC中英文说明书

10000+ 代码库、3000+ 研发人员大型保险集团的研发效能提升实践

当Steam教育进入个性化信息技术课程

表单文本框的使用(二) 输入过滤(合成事件)
随机推荐
Applet project structure
【刷题记录】1. 两数之和
鸿蒙os第四次学习
中国管理科学研究院凝聚行业专家,傅强荣获智库专家“十佳青年”称号
Cutting edge technology for cultivating robot education creativity
E. Singhal and numbers (prime factor decomposition)
How to form standard interface documents
产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现
Is the securities account given by the school of Finance and business safe? Can I open an account?
Interpreting the daily application functions of cooperative robots
Abnova丨E (DIII) (WNV) 重组蛋白 中英文说明书
Duchefa丨S0188盐酸大观霉素五水合物中英文说明书
教你自己训练的pytorch模型转caffe(三)
从架构上详解技术(SLB,Redis,Mysql,Kafka,Clickhouse)的各类热点问题
ts 之 泛型
Duchefa s0188 Chinese and English instructions of spectinomycin hydrochloride pentahydrate
phpstudy小皮的mysql点击启动后迅速闪退,已解决
AI 从代码中自动生成注释文档
Usaco3.4 "broken Gong rock" band raucous rockers - DP
Rainbow 5.7.1 supports docking with multiple public clouds and clusters for abnormal alarms