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. 09.C语言:预处理(宏定义)、字节序、地址对齐

    一.预处理 预处理 gcc -E Hello.c -o hello.i 编译 gcc -S hello.i -o hello.s 汇编 gcc -c hello.s -o hello.o 链接 gcc ...

  2. Uva 10730 Antiarithmetic?

    uva 10730 题意:给出一列数字,如果其中存在长度大于等于3的等差数列,输出no,不存在就输出yes 这道题标定了数列长度n,而且这n个数数据范围是从0到n-1,没有相同的数,这就给我们枚举提供 ...

  3. CSS中具有继承性的属性:

    CSS中具有继承性的属性:   color:#eee font: font-style font-variant: font-weight:bold font-size font-family: fo ...

  4. 【Codeforces 493C】Vasya and Basketball

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 枚举三分线(离散后)的位置 然后根据预处理的前缀和,快速算出两个队伍的分数. [代码] #include <bits/stdc++.h& ...

  5. 九度oj 题目1046:求最大值

    题目1046:求最大值 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:11782 解决:4789 题目描述: 输入10个数,要求输出其中的最大值. 输入: 测试数据有多组,每组10个数. ...

  6. Eclipse代码/目录虚线对齐设置

    前提: 我的Eclipse版本如下: 比这个版本新或者旧都可以实现如下效果. 实现步骤: 在代码上显示虚线设置有如下方法: 1.如果不使用插件,Eclipse是不支持虚线的,只能是横条的点状,效果如下 ...

  7. 深入解析Microsoft Sql server 2008

    http://blog.csdn.net/downmoon/article/details/5256548

  8. ZOJ 3675 Trim the Nails(bfs)

    Trim the Nails Time Limit: 2 Seconds      Memory Limit: 65536 KB Robert is clipping his fingernails. ...

  9. LeetCode 67. Add Binary (二进制相加)

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

  10. 浅析js的函数的按值传递参数

    js的函数传参的方式是按值传递,正常情况下,改变函数参数的值,并不会对函数外部的变量造成影响.例如: 'use strict';var list = [1, 2, 3]; list.forEach(f ...