Lintcode-medium-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" ).

Given S = "rabbbit" , T = "rabbit" , return 3 .

Dynamic programming, with an int two-dimensional array record: The number of different subsequence in the string s before the first J characters (0-j-1) in the first I character (0-i-1)

State transitions:

In either case, the number of times before the first J character in T in S is at least equal to the number of i-1 characters before the first J characters in T, which contains a special case if j = = I, this value is 0

If the j-1 character of T is different from the i-1 character of S, then the addition of one character in S will not change the result, then dp[i][j] = Dp[i-1][j]

If the same, then dp[i][j] on the basis of dp[i-1][j], but also include T in the first j-1 characters in S before the first i-1 characters in the number of occurrences (that is, the last character of the two strings together, and then consider the previous situation), dp[i][j] = Dp[i-1][j ] + dp[i-1][j-1]

 Public classSolution {/**     * @paramS, t:two string. * @return: Count The number of distinct subsequences*/     Public intnumdistinct (String S, String T) {//Write your code here                if(S = =NULL|| S.length () = = 0)            return0; if(T = =NULL|| T.length () = = 0)            return1; intm =s.length (); intn =t.length (); if(N >m)return0; int[] DP =New int[M + 1] [n + 1]; dp[0][0] = 1;  for(inti = 0; I <= m; i++) dp[i][0] = 1;  for(intj = 1; J <= N; J + +){            CharT_end = T.charat (j-1);  for(inti = j; I <= m; i++){                CharS_end = S.charat (i-1); DP[I][J]= Dp[i-1][j]; if(S_end = =t_end) Dp[i][j]+ = Dp[i-1][j-1]; }        }                returnDp[m][n]; }}

Lintcode-medium-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.