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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[Leetcode][程序员面试金典][面试题17.13][JAVA][恢复空格][动态规划][Trie][字符串哈希]

發(fā)布時(shí)間:2023/12/10 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Leetcode][程序员面试金典][面试题17.13][JAVA][恢复空格][动态规划][Trie][字符串哈希] 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

【問題描述】[中等]

【解答思路】

1. 動(dòng)態(tài)規(guī)劃

動(dòng)態(tài)規(guī)劃流程
第 1 步:設(shè)計(jì)狀態(tài)
dp[i] 表示字符串的前 i 個(gè)字符的最少未匹配數(shù)。

第 2 步:狀態(tài)轉(zhuǎn)移方程
假設(shè)當(dāng)前我們已經(jīng)考慮完了前 i -1個(gè)字符了,對(duì)于前 i 個(gè)字符對(duì)應(yīng)的最少未匹配數(shù):

第 i 個(gè)字符未匹配,則 dp[i] = dp[i+1] + 1,即不匹配數(shù)加 1;
遍歷前 i -1個(gè)字符,若以其中某一個(gè)下標(biāo) j 為開頭、以第 i 個(gè)字符為結(jié)尾的字符串正好在詞典里,則 dp[i] = min(dp[ i ], dp[ j ]) 更新 dp[i]。

第 3 步:考慮初始化
int[] dp = new int[n+1];
dp[0] = 0;
第 4 步:考慮輸出
dp[n];

時(shí)間復(fù)雜度:O(N^3) 空間復(fù)雜度:O(N)

class Solution {public int respace(String[] dictionary, String sentence) {Set<String> dic = new HashSet<>();for(String str: dictionary) dic.add(str);int n = sentence.length();//dp[i]表示sentence前i個(gè)字符所得結(jié)果int[] dp = new int[n+1];for(int i=1; i<=n; i++){dp[i] = dp[i-1]+1; //先假設(shè)當(dāng)前字符作為單詞不在字典中for(int j=0; j<i; j++){if(dic.contains(sentence.substring(j,i))){dp[i] = Math.min(dp[i], dp[j]);}}}return dp[n];} }
2. Trie字典樹優(yōu)化



復(fù)雜度分析

class Solution {public int respace(String[] dictionary, String sentence) {int n = sentence.length();Trie root = new Trie();for (String word: dictionary) {root.insert(word);}int[] dp = new int[n + 1];Arrays.fill(dp, Integer.MAX_VALUE);dp[0] = 0;for (int i = 1; i <= n; ++i) {dp[i] = dp[i - 1] + 1;Trie curPos = root;for (int j = i; j >= 1; --j) {int t = sentence.charAt(j - 1) - 'a';if (curPos.next[t] == null) {break;//單詞終結(jié)標(biāo)志} else if (curPos.next[t].isEnd) {dp[i] = Math.min(dp[i], dp[j - 1]);}if (dp[i] == 0) {break;}curPos = curPos.next[t];}}return dp[n];} }class Trie {public Trie[] next;public boolean isEnd;public Trie() {next = new Trie[26];isEnd = false;}public void insert(String s) {Trie curPos = this;for (int i = s.length() - 1; i >= 0; --i) {int t = s.charAt(i) - 'a';if (curPos.next[t] == null) {curPos.next[t] = new Trie();}curPos = curPos.next[t];}//給遍歷的時(shí)候的單詞終結(jié)標(biāo)志curPos.isEnd = true;} }
3. 字符串哈希


復(fù)雜度分析

class Solution {static final long P = Integer.MAX_VALUE;static final long BASE = 41;public int respace(String[] dictionary, String sentence) {Set<Long> hashValues = new HashSet<Long>();for (String word : dictionary) {hashValues.add(getHash(word));}int[] f = new int[sentence.length() + 1];Arrays.fill(f, sentence.length());f[0] = 0;for (int i = 1; i <= sentence.length(); ++i) {f[i] = f[i - 1] + 1;long hashValue = 0;for (int j = i; j >= 1; --j) {int t = sentence.charAt(j - 1) - 'a' + 1;hashValue = (hashValue * BASE + t) % P;if (hashValues.contains(hashValue)) {f[i] = Math.min(f[i], f[j - 1]);}}}return f[sentence.length()];}public long getHash(String s) {long hashValue = 0;for (int i = s.length() - 1; i >= 0; --i) {hashValue = (hashValue * BASE + s.charAt(i) - 'a' + 1) % P;}return hashValue;} }

【總結(jié)】

1.動(dòng)態(tài)規(guī)劃流程

第 1 步:設(shè)計(jì)狀態(tài)
第 2 步:狀態(tài)轉(zhuǎn)移方程
第 3 步:考慮初始化
第 4 步:考慮輸出
第 5 步:考慮是否可以狀態(tài)壓縮

2. Rabin-Karp 字符串編碼 (字符串哈希)

3.Trie
class Trie {public Trie[] next;public boolean isEnd;public Trie() {next = new Trie[26];isEnd = false;}public void insert(String s) {Trie curPos = this;for (int i = s.length() - 1; i >= 0; --i) {int t = s.charAt(i) - 'a';if (curPos.next[t] == null) {curPos.next[t] = new Trie();}curPos = curPos.next[t];}curPos.isEnd = true;} }

轉(zhuǎn)載鏈接:https://leetcode-cn.com/problems/re-space-lcci/solution/hui-fu-kong-ge-by-leetcode-solution/
Rabin-Karp 字符串編碼 參考鏈接:https://leetcode-cn.com/problems/longest-happy-prefix/solution/zui-chang-kuai-le-qian-zhui-by-leetcode-solution/

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的[Leetcode][程序员面试金典][面试题17.13][JAVA][恢复空格][动态规划][Trie][字符串哈希]的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。