摘要:用動規(guī)方法做建立長度為和的二維數(shù)組,表示的第到位子串包含不同的的第到位子串的個數(shù)。初始化當(dāng)?shù)淖哟L度為時,當(dāng)?shù)淖哟L度為時,當(dāng)和子串都為時,包含,故。
Problem
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).
ExampleGiven S = "rabbbit", T = "rabbit", return 3.
ChallengeDo it in O(n2) time and O(n) memory.
O(n2) memory is also acceptable if you do not know how to optimize memory.
Note用動規(guī)方法做:
建立長度為m+1和n+1的二維dp數(shù)組,dp[i][j]表示S的第0到i位子串包含不同的T的第0到j(luò)位子串的個數(shù)。
初始化:當(dāng)T的子串長度為0時,dp[i][0] = 1;當(dāng)S的子串長度為0時,dp[0][i] = 0;當(dāng)S和T子串都為0時,0包含0,故dp[0][0] = 1。
兩次循環(huán)S和T,若S的最后一位和T的最后一位不同,那么S增加一位不會增加更多的包含關(guān)系,即dp[i][j]仍然等于dp[i-1][j]。若S的最后一位和T的最后一位相等,最后的結(jié)果一定也包含S和T各減一位的結(jié)果,如S="abb",T="ab",所以dp[i][j]包含dp[i-1][j-1],再與與dp[i-1][j]相加。
見下面的例子。
0 A B C 0 1 0 0 0 C 1 0 0 0 A 1 1 0 0 B 1 1 1 0 B 1 1 2 0 B 1 1 3 0 C 1 1 3 3Solution
public class Solution { public int numDistinct(String S, String T) { int m = S.length(), n = T.length(); if (m < n) return 0; int[][] dp = new int[m+1][n+1]; for (int i = 0; i <= m; i++) dp[i][0] = 1; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { dp[i][j] = dp[i-1][j]; if (S.charAt(i-1) == T.charAt(j-1)) dp[i][j] += dp[i-1][j-1]; } } return dp[m][n]; } }
One-dimension DP, O(n) space
public class Solution { public int numDistinct(String s, String t) { int m = s.length(); int n = t.length(); int[] dp = new int[n+1]; dp[0] = 1; for (int i = 1; i <= m; i++) { for (int j = n; j >= 1; j--) { if (s.charAt(i-1) == t.charAt(j-1)) { dp[j] += dp[j-1]; } } } return dp[n]; } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/65796.html
摘要:計算元素值時,當(dāng)末尾字母一樣,實際上是左方數(shù)字加左上方數(shù)字,當(dāng)不一樣時,就是左方的數(shù)字。示意圖代碼如果這個字符串有個怎么辦用暴力法,對每一位開始向后檢查是否是。 Distinct Subsequences Given a string S and a string T, count the number of distinct subsequences of T in S. A su...
摘要:簡單的動規(guī)題目,建立數(shù)組。坐標(biāo)為矩陣的坐標(biāo),值為從左上角到這一格的走法總數(shù)。賦初值,最上一行和最左列的所有格子的走法都只有一種,其余格子的走法等于其左邊格子走法與上方格子走法之和。最后,返回即可。 Problem A robot is located at the top-left corner of a m x n grid (marked Start in the diagram ...
摘要:和完全一樣的做法,只要在初始化首行和首列遇到時置零且即可。對了,數(shù)組其它元素遇到也要置零喏,不過就不要啦。 Problem Follow up for Unique Paths: Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle...
摘要:題目要求判斷字符串中通過刪減單詞含有幾個字符串。例如中含有個字符串,通過分別刪除第,,個。也就是說,我們需要通過一個數(shù)據(jù)結(jié)構(gòu)來記錄臨時結(jié)果從而支持我們在已知前面幾個情況的場景下對后續(xù)情況進(jìn)行計算。 題目要求 Given a string S and a string T, count the number of distinct subsequences of S which equa...
摘要:終于見到一個使用動態(tài)規(guī)劃的題目了,似乎這種字符串比對的差不多都是的思路。從后向前遞推,我們可以得到下面的矩陣可以看出,矩陣中每個的數(shù)值為,這樣右下角的值即為所求。 Problem Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of...
閱讀 1718·2021-10-18 13:34
閱讀 3906·2021-09-08 10:42
閱讀 1551·2021-09-02 09:56
閱讀 1606·2019-08-30 15:54
閱讀 3127·2019-08-29 18:44
閱讀 3298·2019-08-26 18:37
閱讀 2212·2019-08-26 12:13
閱讀 454·2019-08-26 10:20