Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Toshihiro Shimizu authored and Toshihiro Shimizu committed Dec 8, 2024
1 parent fd885db commit 676a61e
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/atcoder/extra/string/run_length_compress.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
when not declared ATCODER_RUN_LENGTH_COMPRESS_HPP:
const ATCODER_RUN_LENGTH_COMPRESS_HPP* = 1
import strutils
proc encode*(s:string):seq[tuple[c:char, n:int]] =
var i = 0
while i < s.len:
var j = i
while j < s.len and s[i] == s[j]: j.inc
result.add (s[i], j - i)
i = j

proc decode*(a:seq[tuple[c:char, n:int]]):string =
for (c, n) in a:
result.add c.repeat(n)

proc encode*[T](s:seq[T]):seq[tuple[c:T, n:int]] =
var i = 0
while i < s.len:
var j = i
while j < s.len and s[i] == s[j]: j.inc
result.add (s[i], j - i)
i = j

proc decode*[T](a:seq[tuple[c:T, n:int]]):seq[T] =
for (c, n) in a:
result.add c.repeat(n)

0 comments on commit 676a61e

Please sign in to comment.