Leetcode-distinct Sequences

Source: Internet
Author: User
Tags add numbers
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.