Use DP, dp[i][j] means the number of substrings of t before index i in the substring of s before index j. When I loop the dp array, I have two conditions that t[i]==s[j] and t[i]!=s[j]. If t[i]==s[j], this s[j] can be considered or ignored, which means dp[i][j]=dp[i-1][j-1]+dp[i][j-1]. If t[i]!=s[j], this s[j] must be ignored, so dp[i][j]=dp[i][j-1].
class Solution {
public:
int numDistinct(string s, string t) {
int slen=s.length();
int tlen=t.length();
if(tlen>slen)
return 0;
vector<int> a(slen,0);
vector<vector<int> > dp(tlen,a);
dp[0][0]=s[0]==t[0];
for(int i=1;i<slen;i++)
dp[0][i]=s[i]==t[0]?dp[0][i-1]+1:dp[0][i-1];
for(int i=1;i<tlen;i++)
for(int j=1;j<slen;j++)
{
if(t[i]==s[j])
dp[i][j]=dp[i-1][j-1]+dp[i][j-1];//s[j] is either considered or ignored
else
dp[i][j]=dp[i][j-1];//s[j] is ignored
}
return dp[tlen-1][slen-1];
}
};