日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/2/28 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 leetcode 115. Distinct Subsequences Hard | 115. 不同的子序列(动态规划) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目

https://leetcode.com/problems/distinct-subsequences/

題解

方法1:遞歸(超時)

這種解法比較容易理解,時間復雜度沒算出來,但肯定不是 O(m*n)。提交之后超時了。

2021-9-20 09:33:04 補充:根據左神的“從暴力遞歸到動態規劃”,可以給此方法加傻緩存,即可得到動態規劃解法。

class Solution {public int numDistinct(String s, String t) {char[] ss = s.toCharArray();char[] tt = t.toCharArray();int count = count(ss, tt, 0, 0);return count;}public int count(char[] ss, char[] tt, int sBegin, int tBegin) {if (tBegin == tt.length) return 1;if (sBegin == ss.length) return 0;int cnt = 0;while (sBegin != ss.length) {if (ss[sBegin] == tt[tBegin]) {cnt += count(ss, tt, sBegin + 1, tBegin + 1); // 固定當前字母并調子過程}// 不固定當前字母繼續搜索sBegin++;}// System.out.println("cnt2=" + cnt);return cnt;} }

方法2:動態規劃

參考:https://leetcode.com/problems/distinct-subsequences/discuss/37327/Easy-to-understand-DP-in-Java

The idea is the following:

  • we will build an array mem where mem[i+1][j+1] means that S[0..j] contains T[0..i] that many times as distinct subsequences. Therefor the result will be mem[T.length()][S.length()].
  • we can build this array rows-by-rows:
  • the first row must be filled with 1. That’s because the empty string is a subsequence of any string but only 1 time. So mem[0][j] = 1 for every j. So with this we not only make our lives easier, but we also return correct value if T is an empty string.
  • the first column of every rows except the first must be 0. This is because an empty string cannot contain a non-empty string as a substring – the very first item of the array: mem[0][0] = 1, because an empty string contains the empty string 1 time.

So the matrix looks like this:

S 0123....j T +----------+|1111111111| 0 |0 | 1 |0 | 2 |0 | . |0 | . |0 | i |0 |

From here we can easily fill the whole grid: for each (x, y), we check if S[x] == T[y] we add the previous item and the previous item in the previous row, otherwise we copy the previous item in the same row. The reason is simple:

  • if the current character in S doesn’t equal to current character T, then we have the same number of distinct subsequences as we had without the new character.
  • if the current character in S equal to the current character T, then the distinct number of subsequences = the same number we had before plus the distinct number of subsequences we had with less longer T and less longer S.

An example:
S: [acdabefbc] and T: [ab]

first we check with a:

* *S = [acdabefbc] mem[1] = [0111222222]

then we check with ab:

* * S = [acdabefbc] mem[1] = [0111222222] mem[2] = [0000022244]

And the result is 4, as the distinct subsequences are:

S = [a b ]S = [a b ]S = [ ab ]S = [ a b ]

See the code in Java:

public int numDistinct(String S, String T) {// array creationint[][] mem = new int[T.length()+1][S.length()+1];// filling the first row: with 1sfor(int j=0; j<=S.length(); j++) {mem[0][j] = 1;}// the first column is 0 by default in every other rows but the first, which we need.for(int i=0; i<T.length(); i++) {for(int j=0; j<S.length(); j++) {if(T.charAt(i) == S.charAt(j)) {mem[i+1][j+1] = mem[i][j] + mem[i+1][j];} else {mem[i+1][j+1] = mem[i+1][j];}}}return mem[T.length()][S.length()]; }

Sure let’s consider the same example as above: S = [acdabefbc], T = [ab]

* * S = [acdabefbc] mem[1] = [0111222222] mem[2] = [00000222_ ]

Imagine that we are filling the gap at _. That means i=1, so T[i] = b and j=7, so S[j] = b.

We’re looking for mem[i+1][j+1], which is the place for _. Currently we know that at this position we have 2 as before, because mem[1][7] = 2, which is the position ABOVE and LEFT to _. Also we know that so far we had 2 subsequences before (namely AcdaBef and acdABef – highlighted with uppercase) because mem[2][7] = 2, which is LEFT to _. So having this new b would increase the number of subsequences (currently 2) with a number of 2, because it can be matched with the 2 as we saw before. That’s why if T[i] == S[j] then mem[i+1][j+1] := mem[i][j] + mem[i+1][j]. So _ will be 4.

總結一下就是:當前 i, j 位置不同子序列數量 = 包含當前字母之前的 [i-1][j-1] 位置已有的不同子序列數量(之前缺當前字母,所以不是完整的子序列,但由于當前字母相同,所以構成并增加了新的可能情況) + 如果當前字母不相同的話 [i][j-1] 位置已經有的的子序列數量(被繼承過來作為累加)

I hope this helped.

自己實現了一遍:

class Solution {public int numDistinct(String s, String t) {int[][] dp = new int[t.length() + 1][s.length() + 1]; // dp[i][j]表示t[:i]在s[:j]范圍內的不同子序列數量for (int i = 0; i < s.length() + 1; i++) {dp[0][i] = 1; // "" 是任何序列的子序列}for (int i = 0; i < t.length(); i++) {dp[i + 1][0] = 0; // "" 不包含任何子序列for (int j = 0; j < s.length(); j++) {if (s.charAt(j) == t.charAt(i)) {// dp[i+1][j+1] = 不選定當前字母時繼承前面已有的子序列數量 + 選定當前字母時與前一個不包含此字母的子序列拼接而產生的新的子序列數量dp[i + 1][j + 1] = dp[i + 1][j] + dp[i][j];}else {dp[i + 1][j + 1] = dp[i + 1][j];}}}return dp[t.length()][s.length()];} }

總結

以上是生活随笔為你收集整理的leetcode 115. Distinct Subsequences Hard | 115. 不同的子序列(动态规划)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 精品人妻无码专区视频 | 黄色一级大片在线免费看产 | 1769国产精品视频 | 中文字幕一区二区三区日韩精品 | 大奶骚 | 欧美精选一区二区 | 伊人精品一区二区三区 | 欧美第二页| 尤物91| 国产精选av | www.黄色. | 去毛片| 69视频污| 亚洲老女人av| 性欧美极品 | 少妇被躁爽到高潮 | 久久久黄色| av国产网站 | 欧美日韩中文字幕在线 | 国产理论视频 | 国产另类精品 | 99热亚洲精品| 性欢交69精品久久久 | 欧美性粗暴 | 欧美性猛交99久久久久99按摩 | 日本在线视频www | 国产成人综合网 | 免费在线观看a视频 | 欧美小视频在线观看 | 福利国产视频 | av在线激情 | 新91在线 | 天天色天天| 欧美色图首页 | 亚洲免费播放 | 亚洲一区 中文字幕 | www国产在线 | 亚洲一区二区网站 | 色妺妺视频网 | 亚洲美女一区二区三区 | 男女69视频 | 欧美日本一区二区三区 | 91久精品 | 性色视频在线观看 | 欧美日韩精品二区 | 在线日韩免费 | 亚洲精品97久久中文字幕无码 | 韩日av网站 | 国产精品欧美一区喷水 | 色爽 av| 精品午夜久久 | 天天骑夜夜操 | 国产手机av在线 | 91久久国语露脸精品国产高跟 | 亚洲国产精品视频一区 | 毛片av在线| 人妻巨大乳hd免费看 | 天天艹天天射 | 成人免费视频国产免费麻豆 | 淫久久 | 日本三不卡 | 婷婷国产在线 | av操操 | 91成人免费 | 国产亚洲一区二区在线 | 美女自卫网站 | 国产精品久久久久久久久久东京 | 深爱五月激情五月 | 在线视频久 | 黄色福利社| 激情六月综合 | 亚洲女同视频 | 天美视频在线观看 | 99免费在线观看 | 免费黄色av | 欧美野外猛男的大粗鳮 | 欧美在线xxxx| 深夜成人福利 | 柠檬av导航 | 亚洲视频成人 | 男女做那个视频 | 女~淫辱の触手3d动漫 | 午夜极品 | 中文在线免费看视频 | 国产又粗又长 | 免费污网站在线观看 | 人妻精品久久久久中文字幕69 | 国产视频麻豆 | 黄色网免费看 | 成年性生交大片免费看 | 青青久久久 | 国产av不卡一区 | 国产91av在线 | 67194午夜| 爱爱综合网 | 成人亚洲 | 中文字幕在线观看欧美 | 日韩精品一区中文字幕 | 手机av在线不卡 |