力扣115:不同的子序列
https://leetcode-cn.com/problems/distinct-subsequences/
题解:
AC代码
class Solution {
public:
int numDistinct(string s, string t) {
int sl = s.size();
int tl = t.size();
long dp[sl+1][tl+1];
for(int j = 0; j <= tl; j++) {
dp[0][j] = 0;
}
for(int i = 0; i <= sl; i++) {
dp[i][0] = 1;
}
for(int i = 1; i <= sl; i++) {
for(int j = 1; j <= tl; j++) {
if(s[i-1] == t[j-1]) {
dp[i][j] = dp[i-1][j-1] + dp[i-1][j];
}
else {
dp[i][j] = dp[i-1][j];
}
}
}
return dp[sl][tl];
}
};