动态规划-Distinct Subsequences

2020-01-03 13:29:04

问题描述:

问题求解:

经典的动态规划题目,一般来说dp题目是递推关系公式难想,但是实际代码量还是比较少的。

有尝试过dfs来做,但是由于时间复杂度是指数级别的,所以会TLE。

    public int numDistinct(String s, String t) {
        int n1 = s.length();
        int n2 = t.length();
        int[][] dp = new int[n2 + 1][n1 + 1];
        for (int i = 0; i <= n1; i++) dp[0][i] = 1;
        for (int i = 1; i <= n2; i++) {
            for (int j = 1; j <= n1; j++) {
                if (t.charAt(i - 1) == s.charAt(j - 1)) dp[i][j] = dp[i - 1][j - 1] + dp[i][j - 1];
                else dp[i][j] = dp[i][j - 1];
            }
        }
        return dp[n2][n1];
    }

  

原文地址:https://www.cnblogs.com/hyserendipity/p/12144470.html

时间: 2024-11-13 09:30:56

动态规划-Distinct Subsequences的相关文章

LeetCode 笔记22 Distinct Subsequences 动态规划需要冷静

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 di

leetcode_115题——Distinct Subsequences(动态规划)

Distinct Subsequences Total Accepted: 31427 Total Submissions: 120395My 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

Distinct Subsequences(不同子序列的个数)——b字符串在a字符串中出现的次数、动态规划

Given a string S and a string T, count the number of distinct subsequences ofT inS. 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 po

LeetCode之“动态规划”: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 th

LeetCode115 Distinct Subsequences

Given a string S and a string T, count the number of distinct subsequences of T in S. (Hard) 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 re

[leetcode]Distinct Subsequences @ Python

原题地址:https://oj.leetcode.com/problems/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

leetcode 之 Distinct Subsequences

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 di

LeetCode: Distinct Subsequences

LeetCode: 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

leetcode -day 15 Distinct Subsequences

1.  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 with