Solution: When see question about two strings, DP shoshould be considered first. we can abstract this question to calculate appear times for string T with length I in string s with length j, which can be represented by numbers [I] [J], then through observation and thinking, we can know for numbers [I] [J] It shoshould at least equal the numbers [I] [J-1] And if T. charat (I) = S. charat (J), numbers [I] [J] shocould also be add numbers [I-1] [J-1]
1 class solution 2 {3 Public: 4 int numdistinct (string S, string t) {5 Int slen = S. length (); int tlen = T. length (); 6 vector <int> dp (slen + 1, vector <int> (tlen + 1 )); // DP [I] [J] indicates the subproblem corresponding to the first I of S and the first J characters before t. 7 For (INT I = 0; I <= slen; I ++) DP [I] [0] = 1; 8 for (INT I = 1; I <= slen; I ++) 9 {10 for (Int J = 1; j <= tlen; j ++) 11 {12 if (s [I-1] = T [J-1]) 13 {14 DP [I] [J] = DP [I-1] [J] + dp [I-1] [J-1]; 15} 16 else17 {18 DP [I] [J] = DP [I-1] [J]; 19} 20} 21} 22 return DP [slen] [tlen]; 23} 24 25}; 26 int main () 27 {28 solution s; 29 string STRs ("B"); 30 string strt (""); 31 cout <S. numdistinct (STRs, strt) <Endl; 32 return 0; 33}
Http://rleetcode.blogspot.com/2014/01/distinct-subsequences-java.html
Leetcode-distinct Sequences