Distinct subsequences of leetcode

Source: Internet
Author: User

Distinct subsequences

Given a string s and a string T, count the number of distinct subsequences of T in S.

A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie,"ACE"Is a subsequence"ABCDE"While"AEC"Is not ).

Here is an example:
S ="rabbbit", T ="rabbit"

Return3.

Analysis: the question is to remove several characters from s and then get t.

Dynamic Planning. If DP [I] [J] is set to indicate the first I characters of S, the conversion method for the first J characters of T is obtained by deleting the characters.

(1) If s [I] = T [J], add s [I] and T [J] To s [0 ~ I-1] and T [0 ~ The J-1] can fully replicate the DP [I-1] [J-1] type conversion method. In addition, s [0 ~ I-1] itself (without adding s [I]) can also be converted to T [0 ~ In DP [I-1] [J] Mode ~ J], of course, there may be 0 types, such as S [0 ~ I-1] does not contain T [J], but because I> = J, all s [0 ~ I-1] may contain T [0 ~ J], so there are

DP [I] [J] = DP [I-1] [J-1] + dp [I-1] [J];

(2) If s [I]! = T [J], meaning s [I] cannot be used for conversion to T [0 ~ J], so the method for adding s [I] to the conversion is repeated, so t [J] needs to be from S [0 ~ I-1], so s [0 ~ I] and S [0 ~ I-1] No difference, that is, DP [I] [J] = DP [I-1] [J];

Initialization: DP [I] [0] is set to 1, that is, any length of S. If it is converted to an empty string, only the method of deleting all characters in 1 is required.

The Code is as follows:

Class solution {public: int numdistinct (string S, string t) {int length1 = S. size (), length1 = T. size (), I, j; vector <int> dp (length1 + 1); for (I = 0; I <= length1; ++ I) {vector <int> TMP (lengh2 +); DP [I] = TMP;} for (I = 0; I <= length1; I ++) DP [I] [0] = 1; // initialize for (I = 1; I <= length1; I ++) {for (j = 1; j <= leng22; 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 [length1] [length1] ;}};



Distinct subsequences of leetcode

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.