[LeetCode]Distinct Subsequences,解题报告

Posted yangykaifa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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.


思路1

開始非常easy想到深搜,通过flags数组做标记位。得到子串的个数

代码:

import java.util.Scanner;

public class DistinctSubsequences {
    private static int disNum = 0;

    public static int numDistinct(String S, String T) {
        int[] flags = new int[S.length()];
        int num = 0;

        dfs(num, flags, 0, 0, S, T);

        return disNum;
    }

    public static void dfs(int num, int[] flags, int indexS, int indexT, String S, String T) {
        if (num == T.length()) {
            disNum++;
        } else {
            for (int i = indexS; i < S.length(); i ++) {                
                if (S.charAt(i) == T.charAt(indexT) && flags[i] == 0) {
                    flags[i] = 1;
                    num++;
                    dfs(num, flags, i + 1, indexT + 1, S, T);
                    flags[i] = 0;
                    num--;
                }
            }
        }
    }

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);

        while (cin.hasNext()) {
            String S = cin.nextLine();
            String T = cin.nextLine();
            disNum = 0;

            int res = numDistinct(S, T);

            System.out.println(res);
        }

        cin.close();
    }
}

可是在大集合的时候 Time Limit Exceeded

技术分享

思路2

既然简单的深搜超时,仅仅能考虑略微复杂一点的DP了。

能够參考动态规划经典的样例。最长公共子序列。


这里我採用二维数组int[][] dp来记录匹配子序列的个数,则状态方程为:

dp[0][0] = 1, T和S均为空串
dp[0][1..S.length() - 1] = 1, T为空串,S仅仅有一种子序列匹配
dp[1..T.length() - 1][0] = 0, S为空串
dp[i][j] = dp[i][j - 1] + (T[i - 1] == S[j - 1] ? dp[i - 1][j - 1] : 0)

代码:

public class Solution {
    public static int numDistinct(String S, String T) {
        if (S == null || S.length() == 0) {
            return 0;
        }

        int[][] dp = new int[T.length() + 1][S.length() + 1];

        dp[0][0] = 1;

        for (int i = 1; i <= S.length(); i++) {
            dp[0][i] = 1;
        }

        for (int i = 1; i <= T.length(); i++) {
            dp[i][0] = 0;
        }

        for (int i = 1; i <= T.length(); i++) {
            for (int j = 1; j <= S.length(); j++) {
                if (T.charAt(i - 1) == S.charAt(j - 1)) {
                    dp[i][j] = dp[i][j - 1] + dp[i - 1][j - 1];
                } else {
                    dp[i][j] = dp[i][j - 1];
                }
            }
        }

        return dp[T.length()][S.length()];
    }

}
























以上是关于[LeetCode]Distinct Subsequences,解题报告的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode]题解(python):115-Distinct Subsequences

[动态规划] leetcode 115 Distinct Subsequences

LeetCode Distinct Subsequences

[LeetCode]Distinct Subsequences,解题报告

[LeetCode]Distinct Subsequences,解题报告

[LeetCode]Distinct Subsequences,解题报告