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的更多相关文章

  1. 【LeetCode】115. Distinct Subsequences 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  2. 【一天一道LeetCode】#115. Distinct Subsequences

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  3. 【LeetCode】940. Distinct Subsequences II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  4. 【LeetCode】114. Distinct Subsequences

    Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of  ...

  5. 【leetcode】940. Distinct Subsequences II

    题目如下: Given a string S, count the number of distinct, non-empty subsequences of S . Since the result ...

  6. 【Lintcode】118.Distinct Subsequences

    题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...

  7. 【LeetCode】491. Increasing Subsequences 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 【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 * ...

  9. 【leetcode】491. Increasing Subsequences

    题目如下: 解题思路:这题把我折腾了很久,一直没找到很合适的方法,主要是因为有重复的数字导致结果会有重复.最后尝试用字典记录满足条件的序列,保证不重复,居然Accept了. 代码如下: class S ...

随机推荐

  1. IIS301重定向:将不带www的域名跳转到带www上

    首先你的域名有这两条解析记录 进入服务器IIS,添加2个站点,如下图 第一个正常绑定你的域名:www.baidu.com 第二个绑定不带www的域名:baidu.com 然后点开ncgd-no-www ...

  2. 【15】AngularJS 输入验证

    AngularJS 输入验证 AngularJS 表单和控件可以验证输入的数据. 输入验证 AngularJS 表单和控件可以提供验证功能,并对用户输入的非法数据进行警告.   客户端的验证不能确保用 ...

  3. noip模拟赛 abcd

    [问题描述]有4个长度为N的数组a,b,c,d.现在需要你选择N个数构成数组e,数组e满足a[i]≤e[i]≤b[i]以及 并且使得 最大.[输入格式]输入文件名为abcd.in.输入文件共 N+1 ...

  4. poj 1562 简单深搜

    //搜八个方向即可 #include<stdio.h> #include<string.h> #define N 200 char ma[N][N]; int n,m,vis[ ...

  5. 【网络流24题】最长k可重区间集问题(费用流)

    [网络流24题]最长k可重区间集问题 [问题分析] 最大权不相交路径问题,可以用最大费用最大流解决. [建模方法] 方法1 按左端点排序所有区间,把每个区间拆分看做两个顶点<i.a>< ...

  6. SpringBoot 拦截器--只允许进入登录注册页面,没登录不允许查看其它页面

    SpringBoot注册登录(一):User表的设计点击打开链接 SpringBoot注册登录(二):注册---验证码kaptcha的实现点击打开链接 SpringBoot注册登录(三):注册--验证 ...

  7. 总结懒加载的解决方法(全)org.hibernate.LazyInitializationException: could not initialize proxy - no Session

    如下错误:org.hibernate.LazyInitializationException: could not initialize proxy - no Session 原因是懒加载的问题,因为 ...

  8. Java使用JNI调用DLL库

    JNI是Java自带的方法,不需要引入第三方jar包,优点是因为是java自带的方法,兼容性较好,缺点就是代码书写繁琐 新建Java项目Test --> 新建测试类TestNative,声明本地 ...

  9. SiteMesh2-简介

    简介: SiteMesh类似与ASP.NET的模板页. SiteMesh是由一个基于Web页面布局.装饰以及与现存Web应用整合的框架.它能帮助我们在由大量页面构成的项目中创建一致的页面布局和外观,如 ...

  10. 关于static静态块的使用和static list的使用

    public class CorsConfiguration { /** * Wildcard representing <em>all</em> origins, metho ...