-
-
Notifications
You must be signed in to change notification settings - Fork 8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.3031 (#2317)
No.3031.Minimum Time to Revert Word to Initial State II
- Loading branch information
Showing
6 changed files
with
387 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
solution/3000-3099/3031.Minimum Time to Revert Word to Initial State II/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
class Hashing { | ||
private: | ||
vector<long long> p; | ||
vector<long long> h; | ||
long long mod; | ||
|
||
public: | ||
Hashing(string word, long long base, int mod) { | ||
int n = word.size(); | ||
p.resize(n + 1); | ||
h.resize(n + 1); | ||
p[0] = 1; | ||
this->mod = mod; | ||
for (int i = 1; i <= n; i++) { | ||
p[i] = (p[i - 1] * base) % mod; | ||
h[i] = (h[i - 1] * base + word[i - 1] - 'a') % mod; | ||
} | ||
} | ||
|
||
long long query(int l, int r) { | ||
return (h[r] - h[l - 1] * p[r - l + 1] % mod + mod) % mod; | ||
} | ||
}; | ||
|
||
class Solution { | ||
public: | ||
int minimumTimeToInitialState(string word, int k) { | ||
Hashing hashing(word, 13331, 998244353); | ||
int n = word.size(); | ||
for (int i = k; i < n; i += k) { | ||
if (hashing.query(1, n - i) == hashing.query(i + 1, n)) { | ||
return i / k; | ||
} | ||
} | ||
return (n + k - 1) / k; | ||
} | ||
}; |
32 changes: 32 additions & 0 deletions
32
solution/3000-3099/3031.Minimum Time to Revert Word to Initial State II/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
type Hashing struct { | ||
p []int64 | ||
h []int64 | ||
mod int64 | ||
} | ||
|
||
func NewHashing(word string, base int64, mod int64) *Hashing { | ||
n := len(word) | ||
p := make([]int64, n+1) | ||
h := make([]int64, n+1) | ||
p[0] = 1 | ||
for i := 1; i <= n; i++ { | ||
p[i] = (p[i-1] * base) % mod | ||
h[i] = (h[i-1]*base + int64(word[i-1]-'a')) % mod | ||
} | ||
return &Hashing{p, h, mod} | ||
} | ||
|
||
func (hashing *Hashing) Query(l, r int) int64 { | ||
return (hashing.h[r] - hashing.h[l-1]*hashing.p[r-l+1]%hashing.mod + hashing.mod) % hashing.mod | ||
} | ||
|
||
func minimumTimeToInitialState(word string, k int) int { | ||
hashing := NewHashing(word, 13331, 998244353) | ||
n := len(word) | ||
for i := k; i < n; i += k { | ||
if hashing.Query(1, n-i) == hashing.Query(i+1, n) { | ||
return i / k | ||
} | ||
} | ||
return (n + k - 1) / k | ||
} |
Oops, something went wrong.