115. Distinct Subsequences (String; DP)

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.

class Solution {
public:
    int numDistinct(string S, string T) {
        int tLen = T.length();
        int sLen = S.length();
        if(sLen == 0) return 0;
        vector<vector<int>> dp(sLen,vector<int>(tLen)); //前j+1个T字符可以有多少种S的前i+1个字符变化而来 

        int i=0;
        int j=0;
        if(S[0]==T[0]) dp[0][0] = 1;
        else dp[0][0]=0;
        for(i = 1; i < tLen; i++)
            dp[0][i]=0;
        for(i = 1 ; i < sLen ;i++)
            if(S[i]==T[0])
                dp[i][0] = dp[i-1][0] + 1;
            else
                dp[i][0] = dp[i-1][0]; 

        for(int i = 1;  i < sLen ; i++)
            for(int j = 1; j < tLen ; j++)
                if(S[i]==T[j])
                    dp[i][j] = dp[i-1][j-1] + dp[i-1][j];
                else
                    dp[i][j] = dp[i-1][j];
       return dp[sLen-1][tLen-1];
    }
};
时间: 2024-10-21 22:31:20

115. Distinct Subsequences (String; DP)的相关文章

115 Distinct Subsequences

115 Distinct Subsequences 这道题是dp, 我定义的dp[i][j] 为用t[:i] 和s[:j] 的disitinct subsequence排列数 class Solution: # @param {string} s # @param {string} t # @return {integer} def numDistinct(self, s, t): m, n = len(t), len(s) if m == 0 or n == 0: return 0 dp,cu

115. Distinct Subsequences(js)

115. Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of S which equals T. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the charac

[LeetCode] Distinct Subsequences(DP)

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

uva10069 - Distinct Subsequences(大数+DP)

题目:uva10069 - Distinct Subsequences(大数+DP) 题目大意:给出字符串A , B.问能够在A中找到多少子串B,可以不连续. 解题思路:dp[i][j] 代表从i位开始的B串在从j位开始的A串中能够找到多少种. B[i] == A[j] dp[i][j] = dp[i - 1][j - 1] + dp[i][j - 1]: B[i] != A[j] dp[i][j] = dp[i][j - 1]:边界要处理一下.就是B中只有最后的那个字符来和A匹配的情况要处理一

[leedcode 115] Distinct Subsequences

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

uva 10069 Distinct Subsequences (dp + 大数)

uva 10069 Distinct Subsequences 题目大意:给出两个字符串A和B,找出A中所有与B相同的子字符串. 解题思路:if(A[j?1]==B[i?1]){ dp[i][j]=dp[i][j]+dp[i?1][j?1]; } import java.math.BigInteger; import java.util.Scanner; /** * Created by spzn on 15-3-30. */ public class Main { public static

leetcode 115 Distinct Subsequences ----- java

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

Leetcode 115 Distinct Subsequences 解题报告

Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solution 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

uva 10069 Distinct Subsequences 【dp+大数】

题目:uva 10069 Distinct Subsequences 题意:给出一个子串 x 和母串 s ,求子串在母串中的不同序列的个数? 分析:定义dp[i][j]:x 的前 i 个字母在 s 的前 j 个字母中的出现次数: dp [ i ] [ j ] = dp [ i ] [ j - 1 ] ; if ( x[ i ] == s [ j ] ) dp[i][j]=add(dp[i-1][j-1],dp[i][j]); 注意点:1:出现次数非常大,要用大数加法 2::注意初始化 AC代码: