-
Notifications
You must be signed in to change notification settings - Fork 5
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
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.
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
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) |