【Leetcode】115. Distinct Subsequences
Description:
Given two string S and T, you need to count the number of T's subsequences appeared in S. The fucking problem description is so confusing.
Input:
String s and t
output:
The number
Analysis:
It's a dynamic processing problem. I drew the dynamic processing of counting the seq numbers and then got the correct formula by guessing? :) Most times I work out the final formula by deducing! Then i back to think it's real sense in the problem.
dp[i][j] represents the number of subsequences in string T (ending before index i) are appeared in string S (ending before index j). So, dp can be processed by the follow formula:
= dp[i][j-1] + dp[i-1][j-1] if s[j] == t[i]
dp[i][j]
= dp[i][j-1] if s[j] != t[i]
BYT:
The fucking input size of test cases in serve are ambiguity! So if you create a 2-dimension array in defined size, you will be in trouble. Dynamic structures like vector will be better!
Code:
class Solution {
public:
int numDistinct(string s, string t) {
if(s.length() == || t.length() == ) return ;
//int dp[50][10908];
vector<vector<int>> dp(t.length() + , vector<int>(s.length() + , ));
dp[][] = (t[] == s[])?:;
for(int i = ; i < s.length(); i ++){
if(s[i] == t[]) dp[][i] = dp[][i - ] + ;
else dp[][i] = dp[][i - ];
}
for(int i = ; i < t.length(); i ++){
dp[i][i - ] = ;
for(int j = i; j < s.length(); j ++){
dp[i][j] = (t[i] == s[j])? (dp[i][j - ] + dp[i - ][j - ]):dp[i][j - ];
}
}
return dp[t.length() - ][s.length() - ];
}
};
【Leetcode】115. Distinct Subsequences的更多相关文章
- 【LeetCode】115. Distinct Subsequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【一天一道LeetCode】#115. Distinct Subsequences
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】940. Distinct Subsequences II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【LeetCode】114. Distinct Subsequences
Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of ...
- 【leetcode】940. Distinct Subsequences II
题目如下: Given a string S, count the number of distinct, non-empty subsequences of S . Since the result ...
- 【Lintcode】118.Distinct Subsequences
题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...
- 【LeetCode】491. Increasing Subsequences 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】115. Populating Next Right Pointers in Each Node (2 solutions)
Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeLinkNode * ...
- 【leetcode】491. Increasing Subsequences
题目如下: 解题思路:这题把我折腾了很久,一直没找到很合适的方法,主要是因为有重复的数字导致结果会有重复.最后尝试用字典记录满足条件的序列,保证不重复,居然Accept了. 代码如下: class S ...
随机推荐
- Pygame游戏开发入门(1)-开发框架
pygame库的安装 pip install pygame pygame最小开发框架 #Pygame Hello World Game import pygame,sys #引入pygame和sys( ...
- loadrunner12 + ie11 无internet, 代码中文乱码
第一次用lr 录制的时候显示无internet, 在网上找了好久答案, 无非是ie路径设置,还有证书...... 都试过了不好用,自己研究一下午 , 最后发现是协议没对应上,http协议 ...
- wannafly-day1 Problem B-Board
思路:这个题队友过的,我的思路是枚举行和列,将除了要求位置初始0,每行最小值相减,每列最小值相减,直到除了要求的位置,别的位置都为零,则那个位置取绝对值就行了,有点麻烦应该能过,但是他没有用我给的想法 ...
- [bzoj4521][Cqoi2016][手机号码] (数位dp+记忆化搜索)
Description 人们选择手机号码时都希望号码好记.吉利.比如号码中含有几位相邻的相同数字.不含谐音不 吉利的数字等.手机运营商在发行新号码时也会考虑这些因素,从号段中选取含有某些特征的号 码单 ...
- photon Unity RPC 调用流程
本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/51425225 作者:car ...
- 充当别的mcu的外部存储器(51类)
// 锁存地址 - STC12C5A60S2 reg [15:0]rAddr_51; //存放51单片机传过来的地址 读51地址寄存器 always @ (posedge MCLKout or neg ...
- [置顶]
Linux学习总结(20)——Linux 文件夹结构和作用
/bin 二进制可执行命令 /dev 设备特殊文件 /etc 系统管理和配置文件 /etc/rc.d 启动的配置文件和脚本 /home 用户主目录的基点,比如用户user的主目录就是/home/us ...
- Codeforces 263A. Appleman and Easy Task
A. Appleman and Easy Task time limit per test 1 second memory limit per test 256 megabytes input ...
- 51Nod——T 1631 小鲨鱼在51nod小学
https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1631 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 ...
- Speak a Good Word for SB
今天举行了英语词汇发音交流会,通过这个会议我有了非常大了感触. 一共同拥有三个环节.第一个环节读单词我们组读的单词it.pen.do.stop.think.park.sink.wood.在这一个环节中 ...