题目

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()];}}

转载于:https://www.cnblogs.com/yangykaifa/p/6903810.html

[LeetCode]Distinct Subsequences,解题报告相关推荐

  1. [LeetCode] Multiply Strings 解题报告

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  2. LeetCode: Sort List 解题报告

    Sort List Sort a linked list in O(n log n) time using constant space complexity. 使用Merge Sort, 空间复杂度 ...

  3. Leetcode Weekly 188 解题报告

    文章目录 Leetcode 1441. 用栈操作构建数组 Leetcode 1442. 形成两个异或相等数组的三元组数目 Leetcode 1443. 收集树上所有苹果的最少时间 Leetcode 1 ...

  4. LeetCode: Maximum Subarray 解题报告

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

  5. leetcode Distinct Subsequences

    字符串处理与二维dp 设置二维数组v[i][j]表示S串中前i个字母的子串中包含多少个T串中前j个字母的子串"这样的问题记为A[i][j]. 可以得到递推式 : if(S[i-1] == T ...

  6. LeetCode: Add Binary 解题报告

    Add Binary Given two binary strings, return their sum (also a binary string). For example, a = " ...

  7. LeetCode 解题报告索引

    最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                        ...

  8. LeetCode:115. Distinct Subsequences

    题目 Given a string S and a string T, count the number of distinct subsequences of S which equals T. A ...

  9. leetcode 115. Distinct Subsequences Hard | 115. 不同的子序列(动态规划)

    题目 https://leetcode.com/problems/distinct-subsequences/ 题解 方法1:递归(超时) 这种解法比较容易理解,时间复杂度没算出来,但肯定不是 O(m ...

最新文章

  1. 【Spring Web MVC】Spring Web MVC 注解开发环境搭建
  2. Row_number () over (partition by col1 order by col2)的用法
  3. assertionerror python_Python成为专业人士笔记–内置模块Modules和函数Functions
  4. 365赚钱养猫小程序
  5. 33 关 Python 游戏,测试你的爬虫能力到底及格不?
  6. 32驱动_轻松掌握pinctrl子系统驱动开发——一个虚拟pinctrl dev驱动开发
  7. 欢迎使用CSDN-markdown编辑器--样例
  8. 一本用户体验时代的产品生存指南
  9. Flutter监听网络变化
  10. 数据库优化之MySQL
  11. MacOS怎么使用分区加密功能?MacOS硬盘分区加密功能使用方法
  12. 2013年深圳百公里徒步感悟
  13. [week13] 2 - T1
  14. 看点直播抓取视频回放链接
  15. 大学计算机基础--1
  16. linux虚拟机中通过挂载iso文件安装jdk
  17. oracle批量粘贴文本,ORACLE 快速批量导入文本数据到数据库(sqlldr工具)方法与分析...
  18. linux创建xfs文件系统命令,通过案例学习xfs文件系统相关命令
  19. Vue Cli 3项目 使用融云IM实现聊天功能
  20. Arduino UNO +74HC164流水灯示例

热门文章

  1. linux的xmgrace无法运行,科学网—安装xmgrace - 林绪波的博文
  2. 做小程序的流程总结(基本篇)
  3. localStorage和sessionStorage的简单使用
  4. Hibernate--使用xml配置映射关系
  5. Eclips将lib打入war中
  6. ubuntu 10.4非法关机后上不了网
  7. libev源码分析--常用的watcher
  8. [译作]Class in Jscript Part I
  9. linux脚本语句,LINUX shell 脚本语句
  10. 是先设计mysql表再进行php代码_PHP与RBAC设计思路,数据表设计与源码讲解