日韩性视频-久久久蜜桃-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. 不同的子序列(动态规划)的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 成人动漫久久 | 久久久久久久美女 | 色婷婷网| 欧美极品一区 | 黄色的视频网站 | 国产亚洲无码精品 | 日本a视频在线观看 | 美女裸体跪姿扒开屁股无内裤 | 四虎精品在线 | 亚洲激情五月婷婷 | 手机在线一区二区 | 91丨国产丨白丝 | 午夜视频在线观看一区二区 | av手机免费看 | 久久婷婷久久 | 我爱av好色| 国产91精品在线观看 | 黄视频网站免费看 | 免费在线毛片 | 国产三级三级在线观看 | 人妻av无码一区二区三区 | 国产精品成人一区二区 | 国产又粗又黄又爽 | 一区二区精品久久 | 国产激情视频在线播放 | 女人和拘做爰正片视频 | 射久久 | 香蕉人妻av久久久久天天 | 欧美草逼网 | 一级做a视频 | 欧美日韩国产精品 | 日本少妇色视频 | 日本三级在线视频 | 色撸撸在线 | 欧美精品国产 | 欧美激情黑白配 | 国产中年熟女高潮大集合 | 亚洲人在线观看视频 | 六月婷婷av| av资源一区 | 中文字幕第2页 | 超级变态重口av番号 | 久热国产精品视频 | 亚洲人在线视频 | 午夜激情福利视频 | 欧美被狂躁喷白浆精品 | 国产一区二区在线视频 | 精品国产大片大片大片 | 国产成人毛毛毛片 | 精品国产一区二区三区无码 | 欧美大片在线免费观看 | 国产精品com | 日本理论视频 | 91视频二区| 黑人干日本少妇 | 伊人影院99| 四虎永久在线精品免费网址 | 欧美成人a交片免费看 | 91av福利视频| 欧美第一视频 | 欧美影音 | 视频在线日韩 | 日韩三级在线观看 | 男女h网站 | 国产网站黄色 | 欧美精品v国产精品v日韩精品 | 97精品自拍| 在线h网站| 久久这里只有 | 欧美色图激情小说 | 久久精品影视 | 欧美老熟妇乱xxxxx | 欧美人妻一区二区 | 夜夜嗨av一区二区 | 国产做爰全免费的视频软件 | 亚洲狠狠婷婷综合久久久久图片 | 久久成人午夜 | 久久天 | 亚洲风情av | 欧美顶级毛片在线播放 | 国产永久免费 | 免费看污黄网站在线观看 | 久久在线免费视频 | 免费黄色在线播放 | 日本一区二区不卡视频 | a无一区二区三区 | 99re8在线精品视频免费播放 | 日本强好片久久久久久aaa | 国产一区二区三区四区五区在线 | 国产精品自拍小视频 | se94se欧美 | 黄色国产免费 | 天天色小说 | 中文字幕日韩视频 | 久久av红桃一区二区小说 | 98自拍视频 | 中文字幕人妻一区 | 亚洲精品久久久乳夜夜欧美 | 色综合综合网 |