From 8d8185282960026abed50852dd1ecd0a9efff528 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Fri, 9 Aug 2024 14:17:04 +0800 Subject: [PATCH] fix: update solutions to lc problem: No.1147 No.1147.Longest Chunked Palindrome Decomposition --- .../README.md | 109 ++---------------- .../README_EN.md | 109 ++---------------- .../Solution.cpp | 30 ++++- .../Solution.go | 25 ++-- .../Solution.java | 31 +++-- .../Solution.py | 24 ++-- .../Solution.ts | 24 ++-- .../Solution2.cpp | 28 +++-- .../Solution2.go | 18 ++- .../Solution2.java | 26 +++-- .../Solution2.py | 17 ++- .../Solution2.ts | 20 ---- .../Solution3.cpp | 39 ------- .../Solution3.go | 33 ------ .../Solution3.java | 39 ------- .../Solution3.py | 32 ----- 16 files changed, 177 insertions(+), 427 deletions(-) delete mode 100644 solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution2.ts delete mode 100644 solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution3.cpp delete mode 100644 solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution3.go delete mode 100644 solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution3.java delete mode 100644 solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution3.py diff --git a/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/README.md b/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/README.md index cd4029c156002..d25ea1ac073d9 100644 --- a/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/README.md +++ b/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/README.md @@ -93,107 +93,6 @@ tags: #### Python3 -```python -class Solution: - def longestDecomposition(self, text: str) -> int: - n = len(text) - if n < 2: - return n - for i in range(n // 2 + 1): - if text[:i] == text[-i:]: - return 2 + self.longestDecomposition(text[i:-i]) - return 1 -``` - -#### Java - -```java -class Solution { - public int longestDecomposition(String text) { - int n = text.length(); - if (n < 2) { - return n; - } - for (int i = 1; i <= n >> 1; ++i) { - if (text.substring(0, i).equals(text.substring(n - i))) { - return 2 + longestDecomposition(text.substring(i, n - i)); - } - } - return 1; - } -} -``` - -#### C++ - -```cpp -class Solution { -public: - int longestDecomposition(string text) { - int n = text.size(); - if (n < 2) return n; - for (int i = 1; i <= n >> 1; ++i) { - if (text.substr(0, i) == text.substr(n - i)) { - return 2 + longestDecomposition(text.substr(i, n - i - i)); - } - } - return 1; - } -}; -``` - -#### Go - -```go -func longestDecomposition(text string) int { - n := len(text) - if n < 2 { - return n - } - for i := 1; i <= n>>1; i++ { - if text[:i] == text[n-i:] { - return 2 + longestDecomposition(text[i:n-i]) - } - } - return 1 -} -``` - -#### TypeScript - -```ts -function longestDecomposition(text: string): number { - const n: number = text.length; - if (n < 2) { - return n; - } - for (let i: number = 1; i <= n >> 1; i++) { - if (text.slice(0, i) === text.slice(n - i)) { - return 2 + longestDecomposition(text.slice(i, n - i)); - } - } - return 1; -} -``` - - - - - - - -### 方法二:字符串哈希 - -**字符串哈希**是把一个任意长度的字符串映射成一个非负整数,并且其冲突的概率几乎为 $0$。字符串哈希用于计算字符串哈希值,快速判断两个字符串是否相等。 - -因此,在方法一的基础上,我们可以使用字符串哈希的方法,在 $O(1)$ 时间内比较两个字符串是否相等。 - -时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串的长度。 - - - -#### Python3 - ```python class Solution: def longestDecomposition(self, text: str) -> int: @@ -343,7 +242,13 @@ function longestDecomposition(text: string): number { -### 方法三 +### 方法二:字符串哈希 + +**字符串哈希**是把一个任意长度的字符串映射成一个非负整数,并且其冲突的概率几乎为 $0$。字符串哈希用于计算字符串哈希值,快速判断两个字符串是否相等。 + +因此,在方法一的基础上,我们可以使用字符串哈希的方法,在 $O(1)$ 时间内比较两个字符串是否相等。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串的长度。 diff --git a/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/README_EN.md b/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/README_EN.md index a9a2ce5a23354..c1fe9fcb664c9 100644 --- a/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/README_EN.md +++ b/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/README_EN.md @@ -91,107 +91,6 @@ The time complexity is $O(n^2)$, and the space complexity is $O(n)$ or $O(1)$. H #### Python3 -```python -class Solution: - def longestDecomposition(self, text: str) -> int: - n = len(text) - if n < 2: - return n - for i in range(n // 2 + 1): - if text[:i] == text[-i:]: - return 2 + self.longestDecomposition(text[i:-i]) - return 1 -``` - -#### Java - -```java -class Solution { - public int longestDecomposition(String text) { - int n = text.length(); - if (n < 2) { - return n; - } - for (int i = 1; i <= n >> 1; ++i) { - if (text.substring(0, i).equals(text.substring(n - i))) { - return 2 + longestDecomposition(text.substring(i, n - i)); - } - } - return 1; - } -} -``` - -#### C++ - -```cpp -class Solution { -public: - int longestDecomposition(string text) { - int n = text.size(); - if (n < 2) return n; - for (int i = 1; i <= n >> 1; ++i) { - if (text.substr(0, i) == text.substr(n - i)) { - return 2 + longestDecomposition(text.substr(i, n - i - i)); - } - } - return 1; - } -}; -``` - -#### Go - -```go -func longestDecomposition(text string) int { - n := len(text) - if n < 2 { - return n - } - for i := 1; i <= n>>1; i++ { - if text[:i] == text[n-i:] { - return 2 + longestDecomposition(text[i:n-i]) - } - } - return 1 -} -``` - -#### TypeScript - -```ts -function longestDecomposition(text: string): number { - const n: number = text.length; - if (n < 2) { - return n; - } - for (let i: number = 1; i <= n >> 1; i++) { - if (text.slice(0, i) === text.slice(n - i)) { - return 2 + longestDecomposition(text.slice(i, n - i)); - } - } - return 1; -} -``` - - - - - - - -### Solution 2: String Hash - -**String hash** is to map a string of any length to a non-negative integer, and its collision probability is almost $0$. String hash is used to calculate the hash value of a string and quickly determine whether two strings are equal. - -Therefore, based on Solution 1, we can use the method of string hash to compare whether two strings are equal in $O(1)$ time. - -The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string. - - - -#### Python3 - ```python class Solution: def longestDecomposition(self, text: str) -> int: @@ -341,7 +240,13 @@ function longestDecomposition(text: string): number { -### Solution 3 +### Solution 2: String Hash + +**String hash** is to map a string of any length to a non-negative integer, and its collision probability is almost $0$. String hash is used to calculate the hash value of a string and quickly determine whether two strings are equal. + +Therefore, based on Solution 1, we can use the method of string hash to compare whether two strings are equal in $O(1)$ time. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string. diff --git a/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution.cpp b/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution.cpp index f085d23656770..3ebb1231799b9 100644 --- a/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution.cpp +++ b/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution.cpp @@ -1,13 +1,31 @@ class Solution { public: int longestDecomposition(string text) { - int n = text.size(); - if (n < 2) return n; - for (int i = 1; i <= n >> 1; ++i) { - if (text.substr(0, i) == text.substr(n - i)) { - return 2 + longestDecomposition(text.substr(i, n - i - i)); + int ans = 0; + auto check = [&](int i, int j, int k) -> bool { + while (k--) { + if (text[i++] != text[j++]) { + return false; + } + } + return true; + }; + for (int i = 0, j = text.size() - 1; i <= j;) { + bool ok = false; + for (int k = 1; i + k - 1 < j - k + 1; ++k) { + if (check(i, j - k + 1, k)) { + ans += 2; + i += k; + j -= k; + ok = true; + break; + } + } + if (!ok) { + ans += 1; + break; } } - return 1; + return ans; } }; \ No newline at end of file diff --git a/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution.go b/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution.go index 4c5d65be86ca3..0c9364120ce05 100644 --- a/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution.go +++ b/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution.go @@ -1,12 +1,19 @@ -func longestDecomposition(text string) int { - n := len(text) - if n < 2 { - return n - } - for i := 1; i <= n>>1; i++ { - if text[:i] == text[n-i:] { - return 2 + longestDecomposition(text[i:n-i]) +func longestDecomposition(text string) (ans int) { + for i, j := 0, len(text)-1; i <= j; { + ok := false + for k := 1; i+k-1 < j-k+1; k++ { + if text[i:i+k] == text[j-k+1:j+1] { + ans += 2 + i += k + j -= k + ok = true + break + } + } + if !ok { + ans++ + break } } - return 1 + return } \ No newline at end of file diff --git a/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution.java b/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution.java index 0c9e6fd429a40..60208724249d1 100644 --- a/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution.java +++ b/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution.java @@ -1,14 +1,31 @@ class Solution { public int longestDecomposition(String text) { - int n = text.length(); - if (n < 2) { - return n; + int ans = 0; + for (int i = 0, j = text.length() - 1; i <= j;) { + boolean ok = false; + for (int k = 1; i + k - 1 < j - k + 1; ++k) { + if (check(text, i, j - k + 1, k)) { + ans += 2; + i += k; + j -= k; + ok = true; + break; + } + } + if (!ok) { + ++ans; + break; + } } - for (int i = 1; i <= n >> 1; ++i) { - if (text.substring(0, i).equals(text.substring(n - i))) { - return 2 + longestDecomposition(text.substring(i, n - i)); + return ans; + } + + private boolean check(String s, int i, int j, int k) { + while (k-- > 0) { + if (s.charAt(i++) != s.charAt(j++)) { + return false; } } - return 1; + return true; } } \ No newline at end of file diff --git a/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution.py b/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution.py index 20fba192cdbc7..30f40ad4ac566 100644 --- a/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution.py +++ b/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution.py @@ -1,9 +1,19 @@ class Solution: def longestDecomposition(self, text: str) -> int: - n = len(text) - if n < 2: - return n - for i in range(n // 2 + 1): - if text[:i] == text[-i:]: - return 2 + self.longestDecomposition(text[i:-i]) - return 1 + ans = 0 + i, j = 0, len(text) - 1 + while i <= j: + k = 1 + ok = False + while i + k - 1 < j - k + 1: + if text[i : i + k] == text[j - k + 1 : j + 1]: + ans += 2 + i += k + j -= k + ok = True + break + k += 1 + if not ok: + ans += 1 + break + return ans diff --git a/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution.ts b/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution.ts index 0fe9ca2eaeb4b..a413f70e20fb5 100644 --- a/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution.ts +++ b/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution.ts @@ -1,12 +1,20 @@ function longestDecomposition(text: string): number { - const n: number = text.length; - if (n < 2) { - return n; - } - for (let i: number = 1; i <= n >> 1; i++) { - if (text.slice(0, i) === text.slice(n - i)) { - return 2 + longestDecomposition(text.slice(i, n - i)); + let ans = 0; + for (let i = 0, j = text.length - 1; i <= j; ) { + let ok = false; + for (let k = 1; i + k - 1 < j - k + 1; ++k) { + if (text.slice(i, i + k) === text.slice(j - k + 1, j + 1)) { + ans += 2; + i += k; + j -= k; + ok = true; + break; + } + } + if (!ok) { + ++ans; + break; } } - return 1; + return ans; } diff --git a/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution2.cpp b/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution2.cpp index 3ebb1231799b9..1130d50a83db6 100644 --- a/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution2.cpp +++ b/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution2.cpp @@ -1,19 +1,27 @@ class Solution { public: int longestDecomposition(string text) { + using ull = unsigned long long; + int n = text.size(); + int base = 131; + ull p[n + 10]; + ull h[n + 10]; + p[0] = 1; + h[0] = 0; + for (int i = 0; i < n; ++i) { + int t = text[i] - 'a' + 1; + p[i + 1] = p[i] * base; + h[i + 1] = h[i] * base + t; + } + int ans = 0; - auto check = [&](int i, int j, int k) -> bool { - while (k--) { - if (text[i++] != text[j++]) { - return false; - } - } - return true; + auto get = [&](int l, int r) { + return h[r] - h[l - 1] * p[r - l + 1]; }; - for (int i = 0, j = text.size() - 1; i <= j;) { + for (int i = 0, j = n - 1; i <= j;) { bool ok = false; for (int k = 1; i + k - 1 < j - k + 1; ++k) { - if (check(i, j - k + 1, k)) { + if (get(i + 1, i + k) == get(j - k + 2, j + 1)) { ans += 2; i += k; j -= k; @@ -22,7 +30,7 @@ class Solution { } } if (!ok) { - ans += 1; + ++ans; break; } } diff --git a/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution2.go b/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution2.go index 0c9364120ce05..a708cefe76019 100644 --- a/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution2.go +++ b/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution2.go @@ -1,8 +1,22 @@ func longestDecomposition(text string) (ans int) { - for i, j := 0, len(text)-1; i <= j; { + n := len(text) + base := 131 + h := make([]int, n+10) + p := make([]int, n+10) + p[0] = 1 + for i, c := range text { + t := int(c-'a') + 1 + p[i+1] = p[i] * base + h[i+1] = h[i]*base + t + } + get := func(l, r int) int { + return h[r] - h[l-1]*p[r-l+1] + } + + for i, j := 0, n-1; i <= j; { ok := false for k := 1; i+k-1 < j-k+1; k++ { - if text[i:i+k] == text[j-k+1:j+1] { + if get(i+1, i+k) == get(j-k+2, j+1) { ans += 2 i += k j -= k diff --git a/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution2.java b/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution2.java index 60208724249d1..fc0350ab7f2d9 100644 --- a/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution2.java +++ b/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution2.java @@ -1,10 +1,23 @@ class Solution { + private long[] h; + private long[] p; + public int longestDecomposition(String text) { + int n = text.length(); + int base = 131; + h = new long[n + 10]; + p = new long[n + 10]; + p[0] = 1; + for (int i = 0; i < n; ++i) { + int t = text.charAt(i) - 'a' + 1; + h[i + 1] = h[i] * base + t; + p[i + 1] = p[i] * base; + } int ans = 0; - for (int i = 0, j = text.length() - 1; i <= j;) { + for (int i = 0, j = n - 1; i <= j;) { boolean ok = false; for (int k = 1; i + k - 1 < j - k + 1; ++k) { - if (check(text, i, j - k + 1, k)) { + if (get(i + 1, i + k) == get(j - k + 2, j + 1)) { ans += 2; i += k; j -= k; @@ -20,12 +33,7 @@ public int longestDecomposition(String text) { return ans; } - private boolean check(String s, int i, int j, int k) { - while (k-- > 0) { - if (s.charAt(i++) != s.charAt(j++)) { - return false; - } - } - return true; + private long get(int i, int j) { + return h[j] - h[i - 1] * p[j - i + 1]; } } \ No newline at end of file diff --git a/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution2.py b/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution2.py index 30f40ad4ac566..2024fa6706a30 100644 --- a/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution2.py +++ b/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution2.py @@ -1,12 +1,25 @@ class Solution: def longestDecomposition(self, text: str) -> int: + def get(l, r): + return (h[r] - h[l - 1] * p[r - l + 1]) % mod + + n = len(text) + base = 131 + mod = int(1e9) + 7 + h = [0] * (n + 10) + p = [1] * (n + 10) + for i, c in enumerate(text): + t = ord(c) - ord('a') + 1 + h[i + 1] = (h[i] * base) % mod + t + p[i + 1] = (p[i] * base) % mod + ans = 0 - i, j = 0, len(text) - 1 + i, j = 0, n - 1 while i <= j: k = 1 ok = False while i + k - 1 < j - k + 1: - if text[i : i + k] == text[j - k + 1 : j + 1]: + if get(i + 1, i + k) == get(j - k + 2, j + 1): ans += 2 i += k j -= k diff --git a/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution2.ts b/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution2.ts deleted file mode 100644 index a413f70e20fb5..0000000000000 --- a/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution2.ts +++ /dev/null @@ -1,20 +0,0 @@ -function longestDecomposition(text: string): number { - let ans = 0; - for (let i = 0, j = text.length - 1; i <= j; ) { - let ok = false; - for (let k = 1; i + k - 1 < j - k + 1; ++k) { - if (text.slice(i, i + k) === text.slice(j - k + 1, j + 1)) { - ans += 2; - i += k; - j -= k; - ok = true; - break; - } - } - if (!ok) { - ++ans; - break; - } - } - return ans; -} diff --git a/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution3.cpp b/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution3.cpp deleted file mode 100644 index 1130d50a83db6..0000000000000 --- a/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution3.cpp +++ /dev/null @@ -1,39 +0,0 @@ -class Solution { -public: - int longestDecomposition(string text) { - using ull = unsigned long long; - int n = text.size(); - int base = 131; - ull p[n + 10]; - ull h[n + 10]; - p[0] = 1; - h[0] = 0; - for (int i = 0; i < n; ++i) { - int t = text[i] - 'a' + 1; - p[i + 1] = p[i] * base; - h[i + 1] = h[i] * base + t; - } - - int ans = 0; - auto get = [&](int l, int r) { - return h[r] - h[l - 1] * p[r - l + 1]; - }; - for (int i = 0, j = n - 1; i <= j;) { - bool ok = false; - for (int k = 1; i + k - 1 < j - k + 1; ++k) { - if (get(i + 1, i + k) == get(j - k + 2, j + 1)) { - ans += 2; - i += k; - j -= k; - ok = true; - break; - } - } - if (!ok) { - ++ans; - break; - } - } - return ans; - } -}; \ No newline at end of file diff --git a/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution3.go b/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution3.go deleted file mode 100644 index a708cefe76019..0000000000000 --- a/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution3.go +++ /dev/null @@ -1,33 +0,0 @@ -func longestDecomposition(text string) (ans int) { - n := len(text) - base := 131 - h := make([]int, n+10) - p := make([]int, n+10) - p[0] = 1 - for i, c := range text { - t := int(c-'a') + 1 - p[i+1] = p[i] * base - h[i+1] = h[i]*base + t - } - get := func(l, r int) int { - return h[r] - h[l-1]*p[r-l+1] - } - - for i, j := 0, n-1; i <= j; { - ok := false - for k := 1; i+k-1 < j-k+1; k++ { - if get(i+1, i+k) == get(j-k+2, j+1) { - ans += 2 - i += k - j -= k - ok = true - break - } - } - if !ok { - ans++ - break - } - } - return -} \ No newline at end of file diff --git a/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution3.java b/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution3.java deleted file mode 100644 index fc0350ab7f2d9..0000000000000 --- a/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution3.java +++ /dev/null @@ -1,39 +0,0 @@ -class Solution { - private long[] h; - private long[] p; - - public int longestDecomposition(String text) { - int n = text.length(); - int base = 131; - h = new long[n + 10]; - p = new long[n + 10]; - p[0] = 1; - for (int i = 0; i < n; ++i) { - int t = text.charAt(i) - 'a' + 1; - h[i + 1] = h[i] * base + t; - p[i + 1] = p[i] * base; - } - int ans = 0; - for (int i = 0, j = n - 1; i <= j;) { - boolean ok = false; - for (int k = 1; i + k - 1 < j - k + 1; ++k) { - if (get(i + 1, i + k) == get(j - k + 2, j + 1)) { - ans += 2; - i += k; - j -= k; - ok = true; - break; - } - } - if (!ok) { - ++ans; - break; - } - } - return ans; - } - - private long get(int i, int j) { - return h[j] - h[i - 1] * p[j - i + 1]; - } -} \ No newline at end of file diff --git a/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution3.py b/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution3.py deleted file mode 100644 index 2024fa6706a30..0000000000000 --- a/solution/1100-1199/1147.Longest Chunked Palindrome Decomposition/Solution3.py +++ /dev/null @@ -1,32 +0,0 @@ -class Solution: - def longestDecomposition(self, text: str) -> int: - def get(l, r): - return (h[r] - h[l - 1] * p[r - l + 1]) % mod - - n = len(text) - base = 131 - mod = int(1e9) + 7 - h = [0] * (n + 10) - p = [1] * (n + 10) - for i, c in enumerate(text): - t = ord(c) - ord('a') + 1 - h[i + 1] = (h[i] * base) % mod + t - p[i + 1] = (p[i] * base) % mod - - ans = 0 - i, j = 0, n - 1 - while i <= j: - k = 1 - ok = False - while i + k - 1 < j - k + 1: - if get(i + 1, i + k) == get(j - k + 2, j + 1): - ans += 2 - i += k - j -= k - ok = True - break - k += 1 - if not ok: - ans += 1 - break - return ans