2019-06-10 15:45:14 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <inttypes.h>
|
2019-06-22 15:16:55 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <io.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
2019-06-10 15:45:14 +00:00
|
|
|
|
2019-06-14 11:34:02 +00:00
|
|
|
void pair(size_t count, uint8_t byte)
|
|
|
|
{
|
2019-06-10 15:45:14 +00:00
|
|
|
static size_t unique_count = 0;
|
|
|
|
static uint8_t unique_data[15];
|
|
|
|
if (count == 1) {
|
|
|
|
unique_data[unique_count++] = byte;
|
|
|
|
assert(unique_count <= 15);
|
2019-06-14 11:34:02 +00:00
|
|
|
}
|
|
|
|
else {
|
2019-06-10 15:45:14 +00:00
|
|
|
assert(count <= 15);
|
|
|
|
uint8_t control = (count << 4) | unique_count;
|
|
|
|
putchar(control);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < unique_count; i++) {
|
|
|
|
putchar(unique_data[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count != 0) {
|
|
|
|
putchar(byte);
|
2019-06-14 11:34:02 +00:00
|
|
|
}
|
|
|
|
else {
|
2019-06-10 15:45:14 +00:00
|
|
|
assert(control == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
unique_count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 11:34:02 +00:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2019-06-10 15:45:14 +00:00
|
|
|
size_t count = 1;
|
|
|
|
uint8_t byte = getchar();
|
|
|
|
int new;
|
2019-06-22 15:16:55 +00:00
|
|
|
size_t position = 0;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
_setmode(0,_O_BINARY);
|
|
|
|
_setmode(1,_O_BINARY);
|
|
|
|
#endif
|
2019-06-10 15:45:14 +00:00
|
|
|
|
|
|
|
while ((new = getchar()) != EOF) {
|
|
|
|
if (byte == new) {
|
|
|
|
count++;
|
2019-06-14 11:34:02 +00:00
|
|
|
}
|
|
|
|
else {
|
2019-06-10 15:45:14 +00:00
|
|
|
pair(count, byte);
|
|
|
|
byte = new;
|
|
|
|
count = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pair(count, byte);
|
|
|
|
pair(0, 0);
|
|
|
|
}
|