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

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

Return 3.

分析:

这道题说得让人不太容易读懂。它的意思是:T作为一个整体,S有多少个subsequences时T。

用的方法是DP:table[i][j]表示当S是0到 i 的substr的时候、T是0到 j 的substr的时候,有多少个。那么怎么得到table[i][j]呢?我们看S[i]和T[j] 是不是相等。如果是,则两个string的最后一个字符match,所以table[i-1][j-1]。但就算两个string的最后一个字符match,也不代表就一定要匹配它们的最后一个字符。所以还要加上table[i-1][j]。所以有了如下代码:

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

但这并不是最优代码:我们在空间上还可以更节省一点。我们可以不用二维的table,而用一维的。

代码:

class Solution {
public:
    int numDistinct(string S, string T) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        
        int record[200];
        for(int i=1; i<200; i++)
        record[i]=0;
        
        record[0]=1;
        for(int i=1; i<=S.size(); i++)
        for(int j=T.size(); j>=1; j--)
        {
            if(S[i-1]==T[j-1])
            record[j]+=record[j-1];
        }
        
        return record[T.size()];
    }
};



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值