【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 ...
随机推荐
- Django-前后台的数据交互
Django 从后台往前台传递数据时有多种方法可以实现. 最简单的后台是这样的: from django.shortcuts import render def main_page(request): ...
- Django-Rest framework中文翻译-generic-views
通用视图 Django的通用视图......被开发为常见用法模式的快捷方式......它们采用视图开发中的某些常见习语和模式并对其进行抽象,以便您可以快速编写数据的常用视图,而无需重复自己. - Dj ...
- Cx的治疗
题目背景 「Cx的故事」众所周知,Cx是一个宇宙大犇.由于Cx在空中花园失足摔下,导致他那蕴含着无穷智慧的大脑受到了严重的损伤,许多的脑神经断裂.于是,Cx的wife(有么?)决定请巴比伦最好的医师治 ...
- poj——2891 Strange Way to Express Integers
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 16839 ...
- Ubuntu 16.04中CPU轮流100%的问题解决
刚装好Ubuntu 16.04,但是观察各个CPU都是轮流100%,如图所示:
- Hibernate插入错误:GenericJDBCException: could not insert:
数据库中一般不能建立user(表名为User)表,将User类改名,又一次建立映射,问题就能够解决 当然,还有还有一种情况.就是类中id类型错误.要设置为Integer型才干够设置自己主动增长,否则也 ...
- C#趣味程序---九九乘法表
using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { for ...
- Android系统编译时遇到的几个.mk的疑惑。
在Android4.2的源代码Build/prduct_config.mk里面遇到几个疑惑: # Convert a short name like "sooner" into t ...
- configure: error: mysql configure failed. Please check config.log for more information.
为php添加mysql模块时报错 configure: error: mysql configure failed. Please check config.log for more informat ...
- Project Euler:Problem 42 Coded triangle numbers
The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangl ...