Distinct Subsequences

题目描述:

Given a string S and a string T, count the number of distinct subsequences ofT inS.

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 of"ABCDE"while "AEC" is not).

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

Return 3.

简单翻译一下,给定两个字符串S和T,求S有多少个不同的子串与T相同。S的子串定义为在S中任意去掉0个或者多个字符形成的串。

递归求解:

首先找到在S中与T的第一个字符相同的字符,从这个字符开始,递归地求S和T剩下的串。T为空串时,返回1。因为空串本身是另外一个串的一个子序列。这个算法实现简单,但是果然不出意料,大集合超时。

Java代码:

[java]  view plain copy
  1. public int numDistinct(String S, String T) {  
  2.   // Start typing your Java solution below  
  3.   // DO NOT write main() function  
  4.   if (S.length() == 0) {  
  5.     return T.length() == 0 ? 1 : 0;  
  6.   }  
  7.   if (T.length() == 0) {  
  8.     return 1;  
  9.   }  
  10.   int cnt = 0;  
  11.   for (int i = 0; i < S.length(); i++) {  
  12.     if (S.charAt(i) == T.charAt(0)) {  
  13.       cnt += numDistinct(S.substring(i + 1), T.substring(1));  
  14.     }  
  15.   }  
  16.   return cnt;  
  17. }  

遇到这种两个串的问题,很容易想到DP。但是这道题的递推关系不明显。可以先尝试做一个二维的表int[][] dp,用来记录匹配子序列的个数(以 S  = "rabbbit" , T  =  "rabbit" 为例):

    r a b b b i t

  1 1 1 1 1 1 1 1

0 1 1 1 1 1 1 1

a 0 1 1 1 1

b 0 0 2 3 3 3

b 0 0 0 0 3 3 3

i 0 0 0 0 0 0 3 3

t 0 0 0 0 0 0 0 3  

从这个表可以看出,无论T的字符与S的字符是否匹配,dp[i][j] = dp[i][j - 1].就是说,假设S已经匹配了j - 1个字符,得到匹配个数为dp[i][j - 1].现在无论S[j]是不是和T[i]匹配,匹配的个数至少是dp[i][j - 1]。除此之外,当S[j]和T[i]相等时,我们可以让S[j]和T[i]匹配,然后让S[j - 1]和T[i - 1]去匹配。所以递推关系为:

dp[0][0] = 1; // T和S都是空串.

dp[0][1 ... S.length() - 1] = 1; // T是空串,S只有一种子序列匹配。

dp[1 ... T.length() - 1][0] = 0; // S是空串,T不是空串,S没有子序列匹配。

dp[i][j] = dp[i][j - 1] + (T[i - 1] == S[j - 1] ? dp[i - 1][j - 1] : 0).1 <= i <= T.length(), 1 <= j <= S.length()

代码:

class Solution {
public:
    int numDistinct(string S, string T) {
        const int n1 = S.size();
        const int n2 = T.size();
        
        vector<vector<int> > f(n2+1, vector<int>(n1+1, 0));
        f[0][0] = 1;
        
        for(int i=1; i<=n1; i++)
            f[0][i] = 1;
        
        for(int i=1; i<=n2; i++)
        {
            f[i][0] = 0;
            for(int j=1; j<=n1; j++)
            {
                f[i][j] = f[i][j-1];
                if(S[j-1] == T[i-1])
                    f[i][j] += f[i-1][j-1];
            }
        }
        return f[n2][n1];
    }
};


使用滚动数组的优化代码:

class Solution {
public:
    int numDistinct(string S, string T) {
        const int n1 = S.size();
        const int n2 = T.size();
        
        vector<int> f(n1+1, 0);
        
        int upleft;
        
        for(int i=0; i<=n1; i++)
            f[i] = 1;
        
        for(int i=1; i<=n2; i++)
        {
            upleft = f[0];
            f[0] = 0;
            for(int j=1; j<=n1; j++)
            {
                int up = f[j];
                f[j] = f[j-1];
                if(S[j-1] == T[i-1])
                    f[j] += upleft;
                upleft = up;
            }
        }
        return f[n1];
    }
};




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值