问题描述
解题思路
dp[i][j]
表示考虑考虑t
的前j
个字符在s
的前i
个字符中的出现个数:
if (s[i - 1] == t[j - 1]) dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
(表示使用s[i - 1]
匹配和不使用s[i - 1]
匹配)else dp[i][j] = dp[i - 1][j];
代码
class Solution {
public:
int numDistinct(string s, string t) {
if (s.size() < t.size())
return 0;
vector<vector<uint32_t>> dp(s.size() + 1, vector<uint32_t>(t.size() + 1, 0));
// dp[0][0] = 1;
for (int i = 0; i <= s.size(); i++) {
dp[i][0] = 1;
}
for (int i = 1; i <= s.size(); i++) {
for (int j = 1; j <= i && j <= t.size(); j++) {
if (s[i - 1] == t[j - 1])
dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1];
else
dp[i][j] = dp[i - 1][j];
}
}
return dp[s.size()][t.size()];
}
};
标签:distinct,115,int,subsequence,序列,dp,size
From: https://www.cnblogs.com/zwyyy456/p/16841701.html