[Leetcode] Thoughts on solving Distinct subsequences

Source: Internet
Author: User

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 was formed from the original string by deleting some (can be none) of the C Haracters without disturbing the relative positions of the remaining characters. (ie, is a subsequence of and is not "ACE" "ABCDE" "AEC" ).

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

Return 3 .

Question: Given the string s, T, how many different sub-sequences in s are available for T.

The first to see this problem, no ideas, on the Internet to see other people's answers to understand how to solve problems. This is a DP problem, if you want to get the state escape relationship, the answer is more obvious. The solution is a bit like knapsack problem solution.

A two-dimensional array Vv[t.len][s.len] stores intermediate results. Vv[i][k], which represents the number of different sub-sequences in t[0, I] in S[0, K].

State transition Relationship, text description:

When S[i] and t[k] are unequal, the number of different sub-sequences of t[0, I] in S[0, K], and the number of different sub-sequences in S[0, k-1] are the same.

When S[i] and T[k] are at the same time, t[0, I] in S[0, K] The number of different sub-sequences equal to S[i] is taken (vv[i-1][k-1]), plus s[i] is not taken (vv[i][k-1]).

State transition relationships, which are represented by a formula:

When s[i]! = T[k], vv[i][k] = vv[i][k-1]

When s[i] = = T[k], vv[i][k] = Vv[i-1][k-1] + vv[i][k-1]

1 intNumdistinct (stringSstringt) {2     3     if(T.size () >s.size ()) {4         return 0;5     }6 7vector<vector<int>> VV (T.size (), vector<int> (S.size (),-1));8     9     //Boundary Value ProcessingTen      for(inti =1; I < vv.size (); i++) { Onevv[i][i-1] =0; A     } -  -     //Boundary Value Processing the     if(s[0] = = t[0]) { -vv[0][0] =1; -}Else{ -vv[0][0] =0; +     } -      +     //Boundary Value Processing A      for(inti =1; I < vv[0].size (); i++) { at         if(t[0] ==S[i]) { -vv[0][i] = vv[0][i-1] +1; -}Else{ -vv[0][i] = vv[0][i-1]; -         } -     } in      -     //State Transitions to      for(inti =1; I < vv.size (); i++) { +          for(intK = i; K < Vv[i].size (); k++) { -             if(T[i]! =S[k]) { theVv[i][k] = vv[i][k-1]; *}Else{ $Vv[i][k] = vv[i-1][k-1] + vv[i][k-1];Panax Notoginseng             } -         } the     } +      A     returnVv[t.size ()-1][s.size ()-1]; the}

[Leetcode] Thoughts on solving Distinct subsequences

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.