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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

leetcode 467. Unique Substrings in Wraparound String | 467. 环绕字符串中唯一的子字符串(动态规划)

發布時間:2024/2/28 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 leetcode 467. Unique Substrings in Wraparound String | 467. 环绕字符串中唯一的子字符串(动态规划) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目

https://leetcode.com/problems/unique-substrings-in-wraparound-string/

題解

1、dp 超時版本
class Solution {public int findSubstringInWraproundString(String p) {HashSet<Integer>[] arr = new HashSet[26]; // <結尾字母,長度>for (int i = 0; i < 26; i++) {arr[i] = new HashSet();}arr[p.charAt(0) - 'a'].add(1);int curLen = 1; // 避免誤將斷鏈情況計入結果for (int i = 1; i < p.length(); i++) {arr[p.charAt(i) - 'a'].add(1);if (p.charAt(i) == 'a' && p.charAt(i - 1) == 'z') {curLen++;for (int len : arr[25]) {if (len + 1 <= curLen) arr[0].add(len + 1);}} else if (p.charAt(i - 1) == p.charAt(i) - 1) {curLen++;for (int len : arr[p.charAt(i - 1) - 'a']) {if (len + 1 <= curLen) arr[p.charAt(i) - 'a'].add(len + 1);}} else {curLen = 1;}}int result = 0;for (int i = 0; i < 26; i++) {result += arr[i].size();}return result;} }
2、dp 答案版本

參考:Concise Java solution using DP

After failed with pure math solution and time out with DFS solution, I finally realized that this is a DP problem…
The idea is, if we know the max number of unique substrings in p ends with 'a', 'b', ..., 'z', then the summary of them is the answer. Why is that?

  • The max number of unique substring ends with a letter equals to the length of max contiguous substring ends with that letter. Example "abcd", the max number of unique substring ends with 'd' is 4, apparently they are "abcd", "bcd", "cd" and "d".
  • If there are overlapping, we only need to consider the longest one because it covers all the possible substrings. Example: "abcdbcd", the max number of unique substring ends with 'd' is 4 and all substrings formed by the 2nd "bcd" part are covered in the 4 substrings already.
  • No matter how long is a contiguous substring in p, it is in s since s has infinite length.
  • Now we know the max number of unique substrings in p ends with 'a', 'b', ..., 'z' and those substrings are all in s. Summary is the answer, according to the question.
  • Hope I made myself clear…

    class Solution {public int findSubstringInWraproundString(String p) {int[] dp = new int[26]; // <結尾字母,以當前字母結尾的最長串長度>dp[p.charAt(0) - 'a'] = 1;int curLen = 1;for (int i = 1; i < p.length(); i++) {if (p.charAt(i - 1) == p.charAt(i) - 1 || p.charAt(i) == 'a' && p.charAt(i - 1) == 'z') {curLen++;} else {curLen = 1;}dp[p.charAt(i) - 'a'] = Math.max(dp[p.charAt(i) - 'a'], curLen);}int result = 0;for (int i = 0; i < 26; i++) {result += dp[i];}return result;} }

    總結

    以上是生活随笔為你收集整理的leetcode 467. Unique Substrings in Wraparound String | 467. 环绕字符串中唯一的子字符串(动态规划)的全部內容,希望文章能夠幫你解決所遇到的問題。

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

    主站蜘蛛池模板: 日韩三级av| 日本视频在线播放 | 国产成人精品一区二区三区在线观看 | 蜜臀视频在线播放 | 欧美日韩亚洲在线观看 | 欲乱美女| 久久精品国产亚洲AV高清综合 | 神马午夜av | 男人懂的网站 | 国产偷怕 | 日韩电影一二三区 | 被扒开腿一边憋尿一边惩罚 | 国产91熟女高潮一区二区 | 插女生下面| kendra lust free xxx | 久久伊人中文字幕 | 欧美黑人xxxⅹ高潮交 | 男人天堂亚洲 | 午夜网站在线观看 | 国产一级做a | 毛片女人 | 久久99国产综合精品免费 | 亚洲欧美日韩网站 | www.蜜臀av.com| 美女黄色大片 | 国产精九九网站漫画 | 国产一区二区三区高清 | 午夜欧美精品久久久久久久 | 日韩欧美在线第一页 | 亚洲图片欧美在线看 | 在线成人免费电影 | 日韩经典一区二区 | 国产伦精品一区二区三区免费视频 | 日韩欧美国产视频 | 国产av一区二区三区传媒 | 四虎国产精品永久在线国在线 | 在线99热 | 亚洲黄色免费观看 | 极品新婚夜少妇真紧 | 久久久久国产精品无码免费看 | 污污视频在线观看网站 | 手机电影在线观看 | 欧美多p| 超碰婷婷| 色欲久久久天天天综合网精品 | 国产一卡二卡 | 热热热av| 爱爱小视频免费看 | 在线免费看黄色片 | av中文天堂 | 日本女人毛茸茸 | 日韩伦人妻无码 | 一区二区三区偷拍 | 色婷婷狠 | 中文字幕一区二区人妻在线不卡 | 免费成人在线观看动漫 | 1024av在线| 老司机在线看片 | 91直接看 | 日本一级一片免费视频 | 黑人与日本少妇 | 中文字幕亚洲欧美日韩在线不卡 | 亚洲AV无码一区二区伊人久久 | 男生和女生一起差差差很痛的视频 | 老熟妇一区二区三区 | 国产成人无码精品久在线观看 | www.夜夜爱 | 国产成人精品无码免费看在线 | 国产成人免费 | 99热这里只有精 | 成年人网站免费在线观看 | 中文字幕第88页 | 欧美性大战久久久久久久 | 亚洲精品久久久蜜桃 | 日p视频在线观看 | 色狠狠av | 色呦呦日韩精品 | 91成人观看 | 亚洲精品高潮久久久久久久 | 国产黄色录像 | 久久大奶| 久久最新精品 | 亚洲大尺度av| 熟女自拍一区 | 日本中文字幕视频在线 | 国内精品人妻无码久久久影院蜜桃 | 99精品视频在线 | 一级做a爱片 | 爽妇网国产精品 | 亚洲免费在线播放 | 国产日韩一区二区三免费高清 | 黄色aa大片| 调教丰满的已婚少妇在线观看 | 欧美亚洲图片小说 | 国产对白刺激视频 | 欧美精品v国产精品v日韩精品 | 国产av一区二区三区 | 国产伦精品一区二区三区88av | 国产亚洲久一区二区 |