-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7540d75
commit 8bcaf51
Showing
8 changed files
with
143 additions
and
1 deletion.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package combinatorics | ||
|
||
import "github.com/lorenzotinfena/goji/math" | ||
|
||
func HookLength(partition []int, primeMod int) int { | ||
partitionTrasnposed := []int{} | ||
j := 0 | ||
for i := len(partition) - 1; i >= 0; i-- { | ||
for j < partition[i] { | ||
partitionTrasnposed = append(partitionTrasnposed, i+1) | ||
j++ | ||
} | ||
} | ||
|
||
productOfHookLengths := 1 | ||
n := 0 | ||
for i := 0; i < len(partition); i++ { | ||
n += partition[i] | ||
for j := 0; j < partition[i]; j++ { | ||
productOfHookLengths *= partitionTrasnposed[j] - i + partition[i] - j - 1 | ||
productOfHookLengths %= primeMod | ||
} | ||
} | ||
hookLength := math.Factorial(n, primeMod) * math.ModularInverse(productOfHookLengths, primeMod) | ||
hookLength %= primeMod | ||
return (hookLength * hookLength) % primeMod | ||
} |
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,21 @@ | ||
package combinatorics | ||
|
||
// Computes integer partitions, each partition is weakly increasing | ||
func IntegerPartitions(n int) [][]int { | ||
stack := []int{} | ||
partitions := [][]int{} | ||
var computePartitions func(remaining int, maxLength int) | ||
computePartitions = func(remaining int, maxLength int) { | ||
if remaining == 0 { | ||
partitions = append(partitions, append([]int{}, stack...)) | ||
return | ||
} | ||
for i := 1; i <= maxLength && i <= remaining; i++ { | ||
stack = append(stack, i) | ||
computePartitions(remaining-i, i) | ||
stack = stack[:len(stack)-1] | ||
} | ||
} | ||
computePartitions(n, n) | ||
return partitions | ||
} |
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,12 @@ | ||
package combinatorics_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/lorenzotinfena/goji/math/combinatorics" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestIntegerPartition(t *testing.T) { | ||
assert.Equal(t, combinatorics.IntegerPartitions(3), [][]int{{1, 1, 1}, {2, 1}, {3}}) | ||
} |
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 @@ | ||
package misc | ||
|
||
// To get longest odd palidrome starting centered in i: v[2*i+1] | ||
// To get longest even palidrome starting centered between i and i+1: v[2*i+2] | ||
func ManachersAlgorithm[T comparable](v []T) []int { | ||
supp := make([]T, len(v)*2+1) | ||
var foo T | ||
for i := 0; i < len(v); i++ { | ||
supp[2*i] = foo | ||
supp[2*i+1] = v[i] | ||
} | ||
res := make([]int, len(v)*2+1) | ||
l := 1 | ||
r := 1 | ||
for i := 1; i < len(supp); i++ { | ||
if i > r { | ||
l = i | ||
r = i | ||
} | ||
if res[l+r-i] < r-i { | ||
res[i] = res[l+r-i] | ||
} else { | ||
res[i] = r - i | ||
for i+res[i]+1 < len(supp) && i-res[i]-1 >= 0 && supp[i+res[i]+1] == supp[i-res[i]-1] { | ||
res[i]++ | ||
} | ||
l = i - res[i] | ||
r = i + res[i] | ||
} | ||
} | ||
return res | ||
} |
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,13 @@ | ||
package misc_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/lorenzotinfena/goji/misc" | ||
) | ||
|
||
func TestManachersAlgorithms(t *testing.T) { | ||
misc.ManachersAlgorithm([]byte("abcbcbab")) | ||
res := misc.ManachersAlgorithm([]byte("abba")) | ||
t.Log(res[2*1+2]) | ||
} |