JM233333's Blog
  • Programming Languages

    • C
    • UNIX-C
    • Python
  • Algorithms and Data Structures

    • Data Structure
    • Fundamental Algorithms
    • Graph Theory
  • GNU Toolchain

    • Bash
    • gdb
  • Development Environment

    • Ubuntu
    • QEMU
  • Development Tools

    • Git
    • VSCode
  • Operating Systems

    • Principles of Operating Systems
    • Xv6
    • Linux Kernel
  • Software Testing and Analysis

    • Software Testing
    • Software Analysis
    • Program Verification
  • LeetCode
  • XJTUOJ
  • Programming

    • Is Parallel Programming Hard
  • System

    • System Performance
  • Others

    • ...
  • Paper Reading

    • Model Checking
    • Fuzzing
    • Symbolic Execution
  • 3D Game Programming

    • 3D Mathematics
  • Miscellaneous

JM233333

弱小可怜又无助的学术废物
  • Programming Languages

    • C
    • UNIX-C
    • Python
  • Algorithms and Data Structures

    • Data Structure
    • Fundamental Algorithms
    • Graph Theory
  • GNU Toolchain

    • Bash
    • gdb
  • Development Environment

    • Ubuntu
    • QEMU
  • Development Tools

    • Git
    • VSCode
  • Operating Systems

    • Principles of Operating Systems
    • Xv6
    • Linux Kernel
  • Software Testing and Analysis

    • Software Testing
    • Software Analysis
    • Program Verification
  • LeetCode
  • XJTUOJ
  • Programming

    • Is Parallel Programming Hard
  • System

    • System Performance
  • Others

    • ...
  • Paper Reading

    • Model Checking
    • Fuzzing
    • Symbolic Execution
  • 3D Game Programming

    • 3D Mathematics
  • Miscellaneous
  • LeetCode
  • LeetCode 0001 - 0050

  • LeetCode 0051 - 0100

  • LeetCode others

    • LeetCode 0115 - Distinct Subsequences
      • Hints
      • 题面
      • 题意
      • 题解
      • AC代码
    • LeetCode 0206 - Reverse Linked List
  • LeetCode 0001 - xxx
  • leetcode
  • LeetCode others
JM233333
2023-02-13
551

LeetCode 0115 - Distinct Subsequences

Creative Commons

# Hints

  • DP

# 题面

Difficulty Time Complexity Limit Extra-Memory Complexity Limit
Hard O(nm)O(nm)O(nm) −-−

Given two strings sss and ttt, return the number of distinct subsequences of sss which equals ttt.

The test cases are generated so that the answer fits on a 32-bit signed integer.

Example 1:

Input: s = "rabbbit", t = "rabbit"
Output: 3
Explanation:
As shown below, there are 3 ways you can generate "rabbit" from s.
rabbbit
rabbbit
rabbbit

Example 2:

Input: s = "babgbag", t = "bag"
Output: 5
Explanation:
As shown below, there are 5 ways you can generate "bag" from s.

babgbag babgbag babgbag babgbag babgbag

Constraints:

  • 1≤∣s∣,∣t∣≤10001 \le |s|,\ |t| \le 10001≤∣s∣, ∣t∣≤1000

  • sss and ttt consist of English letters.


# 题意

给定两个字符串 sss 和 ttt ,求 sss 中有多少个位置不同的子序列与 ttt 相同。


# 题解

为便于描述,设字符串的下标从 111 起,字符串 sss 和 ttt 的长度分别为 nnn 和 mmm 。

考虑动态规划,设 dp[i][j]dp[i][j]dp[i][j] 表示 s[1,i]s[1,\ i]s[1, i] 中 t[1,j]t[1,\ j]t[1, j] 的出现次数,状态转移方程为:

{dp[i][0]=1,(initialize)dp[0][j]=0,(initialize)dp[i]=dp[i−1][j]+dp[i−1][j−1],(s[i]=t[j])dp[i]=dp[i−1][j],(s[i]≠t[j])\begin{cases} dp[i][0] = 1, &(initialize) \\ dp[0][j] = 0, &(initialize) \\ dp[i] = dp[i - 1][j] + dp[i - 1][j - 1], &(s[i] = t[j]) \\ dp[i] = dp[i - 1][j], &(s[i] \ne t[j]) \end{cases}⎩⎪⎪⎪⎪⎨⎪⎪⎪⎪⎧​dp[i][0]=1,dp[0][j]=0,dp[i]=dp[i−1][j]+dp[i−1][j−1],dp[i]=dp[i−1][j],​(initialize)(initialize)(s[i]=t[j])(s[i]​=t[j])​

则 dp[n][m]dp[n][m]dp[n][m] 即为答案。时间复杂度为 O(nm)O(nm)O(nm) 。


# AC代码

class Solution {
public:
	int numDistinct(const string & s, const string & t) {
		// 初始化
		int n = s.length(), m = t.length();
		int dp[n + 1][m + 1];
		memset(dp, 0, sizeof(dp));
		int mod = 1e9 + 7;
		// 递推
		for (int i = 0; i <= n; i ++) {
			dp[i][0] = 1;
		}
		for (int i = 1; i <= n; i ++) {
			for (int j = 1; j <= i && j <= m; j ++) {
				if (s[i - 1] == t[j - 1]) {
					dp[i][j] = dp[i - 1][j] % mod + dp[i - 1][j - 1] % mod;
				} else {
					dp[i][j] = dp[i - 1][j] % mod;
				}
			}
		}
		// 返回
		return dp[n][m];
	}
};

#LeetCode#LeetCode Hard#DP

← LeetCode 0100 - Same Tree LeetCode 0206 - Reverse Linked List→

最近更新
01
Reading Papers - Partial Order Reduction
04-01
02
Reading Papers - File System Verification
03-01
03
Linux 00 - Introduction
02-01
更多文章>
Theme by Vdoing | Copyright © 2019-2023 JM233333 | CC BY-NC-SA 4.0
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式