|
76440 | 76440 | <ul class="md-nav__list">
|
76441 | 76441 |
|
76442 | 76442 | <li class="md-nav__item">
|
76443 |
| - <a href="#solution-1" class="md-nav__link"> |
| 76443 | + <a href="#solution-1-string-hashing-binary-search-greedy" class="md-nav__link"> |
76444 | 76444 | <span class="md-ellipsis">
|
76445 |
| - Solution 1 |
| 76445 | + Solution 1: String Hashing + Binary Search + Greedy |
76446 | 76446 | </span>
|
76447 | 76447 | </a>
|
76448 | 76448 |
|
@@ -81282,7 +81282,16 @@ <h2 id="description">Description</h2>
|
81282 | 81282 | <h2 id="solutions">Solutions</h2>
|
81283 | 81283 | <!-- solution:start -->
|
81284 | 81284 |
|
81285 |
| -<h3 id="solution-1">Solution 1</h3> |
| 81285 | +<h3 id="solution-1-string-hashing-binary-search-greedy">Solution 1: String Hashing + Binary Search + Greedy</h3> |
| 81286 | +<p>Due to the large data scale of this problem, using the "Trie + Memoization" method will time out. We need to find a more efficient solution.</p> |
| 81287 | +<p>Consider starting from the $i$-th character of the string $\textit{target}$ and finding the maximum matching substring length, denoted as $\textit{dist}$. For any $j \in [i, i + \textit{dist} - 1]$, we can find a string in $\textit{words}$ such that $\textit{target}[i..j]$ is a prefix of this string. This has a monotonic property, so we can use binary search to determine $\textit{dist}$.</p> |
| 81288 | +<p>Specifically, we first preprocess the hash values of all prefixes of strings in $\textit{words}$ and store them in the array $\textit{s}$ grouped by prefix length. Additionally, we preprocess the hash values of $\textit{target}$ and store them in $\textit{hashing}$ to facilitate querying the hash value of any $\textit{target}[l..r]$.</p> |
| 81289 | +<p>Next, we design a function $\textit{f}(i)$ to represent the maximum matching substring length starting from the $i$-th character of the string $\textit{target}$. We can determine $\textit{f}(i)$ using binary search.</p> |
| 81290 | +<p>Define the left boundary of the binary search as $l = 0$ and the right boundary as $r = \min(n - i, m)$, where $n$ is the length of the string $\textit{target}$ and $m$ is the maximum length of strings in $\textit{words}$. During the binary search, we need to check if $\textit{target}[i..i+\textit{mid}-1]$ is one of the hash values in $\textit{s}[\textit{mid}]$. If it is, update the left boundary $l$ to $\textit{mid}$; otherwise, update the right boundary $r$ to $\textit{mid} - 1$. After the binary search, return $l$.</p> |
| 81291 | +<p>After calculating $\textit{f}(i)$, the problem becomes a classic greedy problem. Starting from $i = 0$, for each position $i$, the farthest position we can move to is $i + \textit{f}(i)$. We need to find the minimum number of moves to reach the end.</p> |
| 81292 | +<p>We define $\textit{last}$ to represent the last moved position and $\textit{mx}$ to represent the farthest position we can move to from the current position. Initially, $\textit{last} = \textit{mx} = 0$. We traverse from $i = 0$. If $i$ equals $\textit{last}$, it means we need to move again. If $\textit{last} = \textit{mx}$, it means we cannot move further, so we return $-1$. Otherwise, we update $\textit{last}$ to $\textit{mx}$ and increment the answer by one.</p> |
| 81293 | +<p>After the traversal, return the answer.</p> |
| 81294 | +<p>The time complexity is $O(n \times \log n + L)$, and the space complexity is $O(n + L)$. Here, $n$ is the length of the string $\textit{target}$, and $L$ is the total length of all valid strings.</p> |
81286 | 81295 | <div class="tabbed-set tabbed-alternate" data-tabs="1:4"><input checked="checked" id="__tabbed_1_1" name="__tabbed_1" type="radio" /><input id="__tabbed_1_2" name="__tabbed_1" type="radio" /><input id="__tabbed_1_3" name="__tabbed_1" type="radio" /><input id="__tabbed_1_4" name="__tabbed_1" type="radio" /><div class="tabbed-labels"><label for="__tabbed_1_1">Python3</label><label for="__tabbed_1_2">Java</label><label for="__tabbed_1_3">C++</label><label for="__tabbed_1_4">Go</label></div>
|
81287 | 81296 | <div class="tabbed-content">
|
81288 | 81297 | <div class="tabbed-block">
|
|
0 commit comments