File tree 3 files changed +22
-22
lines changed
3 files changed +22
-22
lines changed Original file line number Diff line number Diff line change @@ -85,16 +85,16 @@ If you are interested in this algorithm, you can check [here][manacher].
85
85
``` typescript
86
86
import manacher from ' @algorithm.ts/manacher'
87
87
88
- export function minCut(s : string ): number {
89
- const N: number = s .length
90
- const f : number [] = manacher (s )
88
+ export function minCut(text : string ): number {
89
+ const N: number = text .length
90
+ const radius : Uint32Array = manacher (text )
91
91
const dp: Uint32Array = new Uint32Array (N )
92
92
93
93
for (let i = 1 ; i < N ; ++ i ) {
94
- let answer: number = i < f [i ] * 2 ? 0 : dp [i - 1 ] + 1
94
+ let answer: number = i < radius [i ] * 2 ? 0 : dp [i - 1 ] + 1
95
95
if (answer > 0 ) {
96
96
for (let k = 1 ; k < i ; ++ k ) {
97
- if (i - k < f [i + k ] * 2 ) {
97
+ if (i - k < radius [i + k ] * 2 ) {
98
98
answer = Math .min (answer , dp [k - 1 ] + 1 )
99
99
}
100
100
}
Original file line number Diff line number Diff line change @@ -39,14 +39,14 @@ test('manacher', function () {
39
39
*/
40
40
function solution1 ( text : string ) : number {
41
41
const N : number = text . length
42
- const f : number [ ] = manacher ( text )
42
+ const radius : Uint32Array = manacher ( text )
43
43
const dp : Uint32Array = new Uint32Array ( N )
44
44
45
45
for ( let i = 1 ; i < N ; ++ i ) {
46
- let answer : number = i < f [ i ] * 2 ? 0 : dp [ i - 1 ] + 1
46
+ let answer : number = i < radius [ i ] * 2 ? 0 : dp [ i - 1 ] + 1
47
47
if ( answer > 0 ) {
48
48
for ( let k = 1 ; k < i ; ++ k ) {
49
- if ( i - k < f [ i + k ] * 2 ) {
49
+ if ( i - k < radius [ i + k ] * 2 ) {
50
50
answer = Math . min ( answer , dp [ k - 1 ] + 1 )
51
51
}
52
52
}
Original file line number Diff line number Diff line change 2
2
* Find the longest palindrome length centered at each position in the given
3
3
* text string within the complexity of $O(N)$.
4
4
*
5
- * Note: len is an array with a length of $2N-1$, which represents the radius
5
+ * Note: radius is an array with a length of $2N-1$, which represents the radius
6
6
* of the palindrome:
7
7
*
8
- * - len [2i] is equal to the radius of the longest palindrome centered at
8
+ * - radius [2i] is equal to the radius of the longest palindrome centered at
9
9
* the position (i, i)
10
10
*
11
- * - len [2i+1] is equal to the radius of the longest palindrome centered at
11
+ * - radius [2i+1] is equal to the radius of the longest palindrome centered at
12
12
* the position (i, i+1)
13
13
*
14
14
* ## Examples
17
17
*
18
18
* ==>
19
19
*
20
- * len [3] = 2 // abba
21
- * len [6] = 2 // bab
20
+ * radius [3] = 2 // abba
21
+ * radius [6] = 2 // bab
22
22
*
23
23
* @param text
24
- * @param len
24
+ * @param radius
25
25
* @param N
26
26
*
27
27
* @see https://me.guanghechen.com/post/algorithm/string/manacher/
28
28
*/
29
- export function manacher ( text : string ) : number [ ] {
29
+ export function manacher ( text : string ) : Uint32Array {
30
30
const N = text . length
31
- const _size = N * 2 - 1
32
- const len : number [ ] = new Array ( _size )
31
+ const _size = ( N << 1 ) - 1
32
+ const radius : Uint32Array = new Uint32Array ( _size )
33
33
34
- len [ 0 ] = 1
34
+ radius [ 0 ] = 1
35
35
for ( let i = 1 , j = 0 ; i < _size ; ++ i ) {
36
36
const p : number = i >> 1
37
37
const q : number = i - p
38
- const r : number = ( ( j + 1 ) >> 1 ) + len [ j ] - 1
38
+ const r : number = ( ( j + 1 ) >> 1 ) + radius [ j ] - 1
39
39
40
- let L = r < q ? 0 : Math . min ( r - q + 1 , len [ ( j << 1 ) - i ] )
40
+ let L = r < q ? 0 : Math . min ( r - q + 1 , radius [ ( j << 1 ) - i ] )
41
41
while ( p > L - 1 && q + L < N && text [ p - L ] === text [ q + L ] ) L += 1
42
- len [ i ] = L
42
+ radius [ i ] = L
43
43
44
44
// Update j
45
45
if ( q + L - 1 > r ) j = i
46
46
}
47
- return len
47
+ return radius
48
48
}
49
49
50
50
export default manacher
You can’t perform that action at this time.
0 commit comments