题干:
力扣
解题报告:
dp[i]代表以s[i]结尾的子序列数量。
class Solution {
public:
int p[20005][26];
long long dp[20005];
const int mod = 1e9 +7;
int distinctSubseqII(string s) {
memset(p[0], -1, sizeof(p[0]));
p[0][s[0]-'a'] = 0;
for(int i = 1; i<s.size(); i++) {
for(int j = 0; j<26; j++) {
if(j == s[i]-'a') p[i][j] = i;
else p[i][j] = p[i-1][j];
}
}
dp[0]=1;
for(int i = 1; i<s.size(); i++) {
dp[i] = 1;
for(int j = 0; j<i; j++) {
if(s[j] != s[i]) dp[i] += dp[j];
}
dp[i] %= mod;
}
int ans = 0;
for(int i = 0; i<s.size(); i++) {
ans = (ans + dp[i])%mod;
}
return ans;
}
};