From 1185350892d65ea98a4ea3e653b7e2b0e2ee49cd Mon Sep 17 00:00:00 2001 From: AC_Oier Date: Tue, 28 Dec 2021 13:09:50 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feat:=20Add=20472?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...46\344\270\262\345\223\210\345\270\214.md" | 1 + "Index/\345\272\217\345\210\227 DP.md" | 1 + ...10\345\233\260\351\232\276\357\274\211.md" | 121 ++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 "LeetCode/471-480/472. \350\277\236\346\216\245\350\257\215\357\274\210\345\233\260\351\232\276\357\274\211.md" diff --git "a/Index/\345\255\227\347\254\246\344\270\262\345\223\210\345\270\214.md" "b/Index/\345\255\227\347\254\246\344\270\262\345\223\210\345\270\214.md" index f3c969c1..3dbfdd72 100644 --- "a/Index/\345\255\227\347\254\246\344\270\262\345\223\210\345\270\214.md" +++ "b/Index/\345\255\227\347\254\246\344\270\262\345\223\210\345\270\214.md" @@ -1,6 +1,7 @@ | 题目 | 题解 | 难度 | 推荐指数 | | ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | -------- | | [187. 重复的DNA序列](https://leetcode-cn.com/problems/repeated-dna-sequences/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/repeated-dna-sequences/solution/gong-shui-san-xie-yi-ti-shuang-jie-hua-d-30pg/) | 中等 | 🤩🤩🤩🤩 | +| [472. 连接词](https://leetcode-cn.com/problems/concatenated-words/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/concatenated-words/solution/gong-shui-san-xie-xu-lie-dpzi-fu-chuan-h-p7no/) | 困难 | 🤩🤩🤩🤩 | | [686. 重复叠加字符串匹配](https://leetcode-cn.com/problems/repeated-string-match/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/repeated-string-match/solution/gong-shui-san-xie-yi-ti-san-jie-qia-chan-3hbr/) | 中等 | 🤩🤩🤩🤩 | | [1044. 最长重复子串](https://leetcode-cn.com/problems/longest-duplicate-substring/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-duplicate-substring/solution/gong-shui-san-xie-zi-fu-chuan-ha-xi-ying-hae9/) | 困难 | 🤩🤩🤩🤩 | diff --git "a/Index/\345\272\217\345\210\227 DP.md" "b/Index/\345\272\217\345\210\227 DP.md" index e283ca9f..a2ff8e65 100644 --- "a/Index/\345\272\217\345\210\227 DP.md" +++ "b/Index/\345\272\217\345\210\227 DP.md" @@ -3,6 +3,7 @@ | [354. 俄罗斯套娃信封问题](https://leetcode-cn.com/problems/russian-doll-envelopes/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/russian-doll-envelopes/solution/zui-chang-shang-sheng-zi-xu-lie-bian-xin-6s8d/) | 困难 | 🤩🤩🤩🤩🤩 | | [368. 最大整除子集](https://leetcode-cn.com/problems/largest-divisible-subset/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/largest-divisible-subset/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-0a3jc/) | 中等 | 🤩🤩🤩🤩 | | [446. 等差数列划分 II - 子序列](https://leetcode-cn.com/problems/arithmetic-slices-ii-subsequence/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/arithmetic-slices-ii-subsequence/solution/gong-shui-san-xie-xiang-jie-ru-he-fen-xi-ykvk/) | 困难 | 🤩🤩🤩🤩🤩 | +| [472. 连接词](https://leetcode-cn.com/problems/concatenated-words/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/concatenated-words/solution/gong-shui-san-xie-xu-lie-dpzi-fu-chuan-h-p7no/) | 困难 | 🤩🤩🤩🤩 | | [583. 两个字符串的删除操作](https://leetcode-cn.com/problems/delete-operation-for-two-strings/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/delete-operation-for-two-strings/solution/gong-shui-san-xie-cong-liang-chong-xu-li-wqv7/) | 中等 | 🤩🤩🤩🤩 | | [629. K个逆序对数组](https://leetcode-cn.com/problems/k-inverse-pairs-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/k-inverse-pairs-array/solution/gong-shui-san-xie-yi-dao-xu-lie-dp-zhuan-tm01/) | 中等 | 🤩🤩🤩🤩🤩 | | [673. 最长递增子序列的个数](https://leetcode-cn.com/problems/number-of-longest-increasing-subsequence/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-longest-increasing-subsequence/solution/gong-shui-san-xie-lis-de-fang-an-shu-wen-obuz/) | 中等 | 🤩🤩🤩🤩 | diff --git "a/LeetCode/471-480/472. \350\277\236\346\216\245\350\257\215\357\274\210\345\233\260\351\232\276\357\274\211.md" "b/LeetCode/471-480/472. \350\277\236\346\216\245\350\257\215\357\274\210\345\233\260\351\232\276\357\274\211.md" new file mode 100644 index 00000000..69e4efb3 --- /dev/null +++ "b/LeetCode/471-480/472. \350\277\236\346\216\245\350\257\215\357\274\210\345\233\260\351\232\276\357\274\211.md" @@ -0,0 +1,121 @@ +### 题目描述 + +这是 LeetCode 上的 **[472. 连接词](https://leetcode-cn.com/problems/concatenated-words/solution/gong-shui-san-xie-xu-lie-dpzi-fu-chuan-h-p7no/)** ,难度为 **困难**。 + +Tag : 「字符串哈希」、「序列 DP」 + + + + +给你一个 不含重复 单词的字符串数组 `words` ,请你找出并返回 `words` 中的所有 连接词 。 + +连接词 定义为:一个完全由给定数组中的至少两个较短单词组成的字符串。 + +示例 1: +``` +输入:words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"] + +输出:["catsdogcats","dogcatsdog","ratcatdogcat"] + +解释:"catsdogcats" 由 "cats", "dog" 和 "cats" 组成; + "dogcatsdog" 由 "dog", "cats" 和 "dog" 组成; + "ratcatdogcat" 由 "rat", "cat", "dog" 和 "cat" 组成。 +``` +示例 2: +``` +输入:words = ["cat","dog","catdog"] + +输出:["catdog"] +``` + +提示: +* $1 <= words.length <= 10^4$ +* $0 <= words[i].length <= 1000$ +* $words[i]$ 仅由小写字母组成 +* $0 <= sum(words[i].length) <= 10^5$ + +--- + +### 序列 DP + 字符串哈希 + +给定数组 $words$,先考虑如何判断某个 $s = words[i]$ 是否为「连接词」。 + +为了方便,我们称组成 `s` 的每个连接部分为 `item`。 + +举个 🌰,例如 `s = abc`,其可能的 `item` 组合为 `a` 和 `bc`。 + +判断单个字符串是否为连接词可使用动态规划求解:**定义 $f[i]$ 为考虑 `s` 的前 $i$ 个字符(令下标从 $1$ 开始),能够切分出的最大 `item` 数的个数。** + +这里之所以采用「记录 $f[i]$ 为最大分割 `item` 数(`int` 类型动规数组)」,而不是「记录 $f[i]$ 为是否可由多个 `item` 组成(`bool` 类型动规数组)」,是因为每个 $s = words[i]$ 至少可由自身组成,采用 `bool` 记录状态的话,最终 $f[n]$ 必然为 `True`,需要额外处理最后一个状态,干脆记录最大分割数量好了。此时如果 `s` 为「连接词」必然有 $f[n] > 1$。 + +不失一般性的考虑 $f[i]$ 该如何转移:**假设 $f[i]$ 可由 $f[j]$ 转移而来(其中 $j < i$),那么能够转移的充要条件为 $f[j] != 0$ 且子串 $s[(j + 1)..i]$ 在 $words$ 出现过**。 + +其中枚举 $i$ 和 $j$ 的复杂度已经去到 $O(n^2)$ 了,如果常规通过 `HashMap` 等数据结构判断某个字符串是否存在,执行哈希函数时需要对字符进行遍历,整体复杂度去到了 $O(n^3)$,会 `TLE`。 + +**我们通过「字符串哈希」方式来优化判断某个子串是否存在于 $words$ 中。** + +具体的,在判断每个 $s = words[i]$ 是否为为连接词前,先对 $words$ 进行遍历,预处理每个 $words[i]$ 的哈希值,并存入 `HashSet` 中,这样我们将「判断某个子串是否存在于 $words$」的问题转化为「判断某个数值是否存在于 `Set` 当中」。 + +又由于 **我们在计算某个子串 `s` 的哈希值时,是从前往后处理每一位的 $s[i]$,因此在转移 $f[i]$ 时,我们期望能够从前往后处理子串,这是常规的从 $[0, i - 1]$ 范围内找可转移点 $f[j]$ 无法做到的**。 + +所以 **我们调整转移逻辑为:从 $f[i]$ 出发,枚举范围 $[i + 1, n]$,找到可由 $f[i]$ 所能更新的状态 $f[j]$,并尝试使用 $f[i]$ 来更新 $f[j]$。转移方程为:** + +$$ +f[j] = \max(f[j], f[i] + 1) +$$ + +当然,能够转移的前提条件为 $f[i]$ 为有效值,且子串 $s[(i + 1), j]$ 在 $words$ 出现过。 + +> 一些细节:为了方便,我们定义 $f[i] = -1$ 为无效状态; +另外由于字符串哈希会产生哈希碰撞,这里在计算哈希值的时候,修改了一下哈希计算方式(额外增加了一个 `OFFSET`),当时的目的是想在电脑没电前 `AC`,而另一个更加稳妥的方式是使用双哈希,或是干脆记录某个哈希值对应了哪些字符串。 + +代码: +```Java +class Solution { + Set set = new HashSet<>(); + int P = 131, OFFSET = 128; + public List findAllConcatenatedWordsInADict(String[] words) { + for (String s : words) { + long hash = 0; + for (char c : s.toCharArray()) hash = hash * P + (c - 'a') + OFFSET; + set.add(hash); + } + List ans = new ArrayList<>(); + for (String s : words) { + if (check(s)) ans.add(s); + } + return ans; + } + boolean check(String s) { + int n = s.length(); + int[] f = new int[n + 1]; + Arrays.fill(f, -1); + f[0] = 0; + for (int i = 0; i <= n; i++) { + if (f[i] == -1) continue; + long cur = 0; + for (int j = i + 1; j <= n; j++) { + cur = cur * P + (s.charAt(j - 1) - 'a') + OFFSET; + if (set.contains(cur)) f[j] = Math.max(f[j], f[i] + 1); + } + if (f[n] > 1) return true; + } + return false; + } +} +``` +* 时间复杂度:令 $n$ 为 $words$ 数组长度,$N = \sum_{i = 0}^{n - 1}words[i].length$,根据数据范围 $N$ 最大为 $1e5$。预处理出 `Set` 的复杂度为 $O(N)$;会对所有 $words[i]$ 执行 `check` 操作,复杂度为 $O((words[i].length)^2)$,总的计算量最大值为 $O(N^2)$,由于存在剪枝,实际上达不到该计算量 +* 空间复杂度:$O(n + \max(words[i].length))$ + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.472` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 +