Skip to content

Commit

Permalink
Merge pull request #11 from harry0000/z-algorithm
Browse files Browse the repository at this point in the history
Add Z algorithm
  • Loading branch information
harry0000 authored Apr 15, 2024
2 parents aca6c19 + ad634f3 commit 10d04c6
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ jobs:
fail-fast: false
matrix:
name: [ "test" ]
scala: [ 3.3.0, 3.3.1 ]
scala: [ 3.3.0, 3.4.1 ]
java: [ 20, 21 ]
include:
- name: "format"
scala: 3.3.1
scala: 3.4.1
java: 21
exclude:
- name: "test"
Expand Down
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
lazy val supportedScalaVersions = List("3.3.1", "3.3.0")
lazy val supportedScalaVersions = List("3.4.1", "3.3.0")

lazy val root = project
.in(file("."))
Expand All @@ -16,6 +16,6 @@ lazy val root = project
"-unchecked",
"-Wunused:all"
),
libraryDependencies += "org.scalameta" %% "munit" % "0.7.29" % Test,
libraryDependencies += "org.scalameta" %% "munit" % "1.0.0-M11" % Test,
Test / parallelExecution := false
)
5 changes: 2 additions & 3 deletions src/main/scala/io/github/acl4s/ModInt.scala
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ object Modulus {
inline def apply[T <: Int](): Modulus[T] = Mod(compiletime.constValue[T])
}

final case class StaticModInt[T <: Int] private (private[this] var _value: Int)(using m: Modulus[T])
extends ModIntBase[T] {
final case class StaticModInt[T <: Int] private (private var _value: Int)(using m: Modulus[T]) extends ModIntBase[T] {
override type Self = StaticModInt[T]
override val mod: T = m.value

Expand Down Expand Up @@ -204,7 +203,7 @@ object ModInt998244353 {
def apply(value: Long): ModInt998244353 = StaticModInt(value)
}

final case class DynamicModInt private (private[this] var _value: Int) extends ModIntBase[Int] {
final case class DynamicModInt private (private var _value: Int) extends ModIntBase[Int] {
override type Self = DynamicModInt
override val mod: Int = DynamicModInt.bt.m

Expand Down
34 changes: 34 additions & 0 deletions src/main/scala/io/github/acl4s/String.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.github.acl4s

import scala.reflect.ClassTag

/**
* Reference:
* D. Gusfield,
* Algorithms on Strings, Trees, and Sequences: Computer Science and
* Computational Biology
*/
private[acl4s] def zAlgorithmImpl[T: ClassTag](s: Array[T]): Array[Int] = {
val n = s.length
if (n == 0) {
return Array.empty
}
val z = new Array[Int](n)
z(0) = 0
var j = 0
for (i <- 1 until n) {
var k = if (j + z(j) <= i) { 0 }
else { Math.min(j + z(j) - i, z(i - j)) }
while (i + k < n && s(k) == s(i + k)) {
k += 1
}
z(i) = k
if (j + z(j) < i + z(i)) {
j = i
}
}
z(0) = n
z
}

def zAlgorithm(s: String): Array[Int] = zAlgorithmImpl(s.toCharArray)
14 changes: 8 additions & 6 deletions src/main/scala/io/github/acl4s/TwoSAT.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.acl4s

import scala.util.boundary, boundary.break

/**
* Reference:
* B. Aspvall, M. Plass, and R. Tarjan,
Expand Down Expand Up @@ -28,12 +30,12 @@ class TwoSAT(private val n: Int) {

def satisfiable(): Boolean = {
val (_, id) = scc.sccIds()
(0 until n).foreach(i => {
if (id(2 * i) == id(2 * i + 1)) {
return false
boundary {
for (i <- 0 until n) {
if (id(2 * i) == id(2 * i + 1)) { break(false) }
answer(i) = id(2 * i) < id(2 * i + 1)
}
answer(i) = id(2 * i) < id(2 * i + 1)
})
true
true
}
}
}
20 changes: 20 additions & 0 deletions src/test/scala/io/github/acl4s/StringSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.github.acl4s

class StringSuite extends munit.FunSuite {

test("zAlgorithm") {
{
val str = "abracadabra";
val lcp = zAlgorithm(str)

assertEquals(lcp.toSeq, Seq(11, 0, 0, 1, 0, 1, 0, 4, 0, 0, 1))
}
{
val str = "ababababa"
val lcp = zAlgorithm(str)

assertEquals(lcp.toSeq, Seq(9, 0, 7, 0, 5, 0, 3, 0, 1))
}
}

}

0 comments on commit 10d04c6

Please sign in to comment.