Skip to content

Commit 8ce9672

Browse files
authored
Update longest-chunked-palindrome-decomposition.cpp
1 parent acfc6e2 commit 8ce9672

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

C++/longest-chunked-palindrome-decomposition.cpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,21 @@
44
// Rabin-Karp Algorithm
55
class Solution {
66
public:
7-
Solution() : pow_(N_ + 1, 1) {
8-
for (int i = 1; i < pow_.size(); ++i) {
9-
pow_[i] = (pow_[i - 1] * D_) % MOD_;
10-
}
11-
}
12-
13-
int longestDecomposition(string text) {
7+
int longestDecomposition(string text) {
8+
static const uint64_t MOD = 1e9 + 7;
9+
static const uint64_t D = 26;
1410
int result = 0;
15-
int l = 0, left = 0, right = 0;
11+
int left = 0, right = 0, l = 0;
12+
uint64_t pow_D = 1ull;
1613
for (int i = 0; i < text.length(); ++i) {
14+
left = (D * left + (text[i] - 'a')) % MOD;
15+
right = (pow_D * (text[text.length() - 1 - i] - 'a') + right) % MOD;
1716
++l;
18-
left = (pow_[1] * left + (text[i] - 'a')) % MOD_;
19-
right = (pow_[l - 1] * (text[text.length() - 1 - i] - 'a') + right) % MOD_;
17+
pow_D = (pow_D * D) % MOD;
2018
if (left == right &&
2119
compare(text, l, i - l + 1, text.length() - 1 - i)) {
2220
++result;
23-
l = 0, left = 0, right = 0;
21+
left = 0, right = 0, l = 0, pow_D = 1;
2422
}
2523
}
2624
return result;
@@ -35,9 +33,4 @@ class Solution {
3533
}
3634
return true;
3735
}
38-
39-
vector<uint64_t> pow_;
40-
static const int N_ = 1000;
41-
static const uint64_t MOD_ = 1e9 + 7;
42-
static const uint64_t D_ = 26;
4336
};

0 commit comments

Comments
 (0)