Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
32
Task/Run-length-encoding/D/run-length-encoding-4.d
Normal file
32
Task/Run-length-encoding/D/run-length-encoding-4.d
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import std.stdio, std.conv, std.array, std.regex, std.utf,
|
||||
std.algorithm;
|
||||
|
||||
string reEncode(string s) {
|
||||
validate(s); // Throw if it's not a well-formed UTF string
|
||||
static string rep(Captures!string m) {
|
||||
auto c = canFind("0123456789#", m[1]) ? "#" ~ m[1] : m[1];
|
||||
return text(m.hit.length / m[1].length) ~ c;
|
||||
}
|
||||
return std.regex.replace!rep(s, regex(`(.|[\n\r\f])\1*`, "g"));
|
||||
}
|
||||
|
||||
|
||||
string reDecode(string s) {
|
||||
validate(s); // Throw if it's not a well-formed UTF string
|
||||
static string rep(Captures!string m) {
|
||||
string c = m[2];
|
||||
if (c.length > 1 && c[0] == '#')
|
||||
c = c[1 .. $];
|
||||
return replicate(c, to!int(m[1]));
|
||||
}
|
||||
auto r=regex(`(\d+)(#[0123456789#]|[\n\r\f]|[^0123456789#\n\r\f]+)`
|
||||
, "g");
|
||||
return std.regex.replace!rep(s, r);
|
||||
}
|
||||
|
||||
void main() {
|
||||
auto s = "尋尋覓覓冷冷清清淒淒慘慘戚戚\nWWWWWWWWWWWWBWWWWWWWWWWW" ~
|
||||
"WBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW\n" ~
|
||||
"11#222##333";
|
||||
assert(s == reDecode(reEncode(s)));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue