题目:115. 不同的子序列 - 力扣(LeetCode)
动态规划问题,f[i][j]表示s的第i个元素匹配到t的第j个元素,有多少种结果
f[i][j] = f[i - 1][j] + (s[i] == t[j] ? f[i - 1][j - 1] : 0)
答案就是 f[s.length() - 1][t.length() - 1]
#define _MAX_ (1000000007)
class Solution {
public:int numDistinct(string s, string t) {int n = (int) s.length();int m = (int) t.length();uint32_t** f = (uint32_t**) malloc(n * sizeof(uint32_t*));for (int i = 0; i < n; i++) {f[i] = (uint32_t*) malloc(m * sizeof(uint32_t));}for (int i = 0; i < n; i++) {if (s[i] == t[0]) {f[i][0] = 1;} else {f[i][0] = 0;}if (i > 0) {f[i][0] += f[i - 1][0];uint32_t a = f[i][0];uint32_t b = f[i - 1][0];if (f[i][0] >= _MAX_) {f[i][0] %= _MAX_;}}for (int j = 1; j < m; j++) {if (i > 0) {f[i][j] = f[i - 1][j];} else {f[i][j] = 0;}if (s[i] == t[j] && i > 0) {f[i][j] += f[i - 1][j - 1];if (f[i][j] >= _MAX_) {f[i][j] %= _MAX_;}}}}
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < m; j++) {
// printf("%d ", f[i][j]);
// }
// printf("\n");
// }return f[n - 1][m - 1];}
};