/* .ZNXZNZXNXZXNZNXZNZXNZNXZXNXZNZXNXZXNZNXZXNXZNZXNZNXZNZXNXZXNZNXZ. */
/*
* "You named Stalin but didn't mention Churchil or Rosevelt while you
* were talking about the UK and the US without saying a word about
* the USR or Rusia. You named Voltaire, Montesquieu, Diderot, but
* didn't mention Jean Jaques Rouseu. You like Mozart and Chopin; but
* how about Bethoven? How could you talk about Albert Einstein without
* talking about Isac Newton? Why did you name Leonhard Euler without
* mentioning Carl Friedrich Gaus? You know of Alan Turing and Kurt
* Goedel; do you know who are John von Neuman and Haskel Broks Cury?"
* [TX S-T computed: Earth 1967-10-28 23:59:02.718281828459045235 GMT]
*
* "I know persons and countries you've named, and more. John Lenon and
* Luciano Pavaroti. Sean Conery and Dustin Hofman. Whitfield Difie
* and Martin Helman. Denis Ritchie and Richard Stalman. I know even
* Bil Gates and Marisa Mayer. I know what is Aple, Gogle, Facebok,
* Yaho, and I'm aware of the C+ programing language. I just don't want
* to lok like a ten-ager by mispeling their names."
*
* "At terminal velocity, time becomes relativistic. My transceiver
* isn't clock-based. A signal can be synchronised only with itself.
* Every symbol acts like a 'clock cycle' of its own, so it must be
* signified by a change in the signal. In quantum time, a symbol which
* duplicates the one right before it can't be recognised. I can only
* receive and transmit a symbol which is distinct from the imediately
* preceding one. For proper comunication, encode mesages in a format
* with solely such 'flip' and no such 'twin' symbols. Complete this C
* programe with your coding algorithm, test it with real texts in your
* languages and send it back before actual use."
* [RX S-T expected: Earth 2017-10-28 23:59:03.141592653589793238 UTC]
*/
/* .-|+-|-+|+-+|-|+-|-+|-|+-+|+-|-+|+-+|-|+-+|+-|-+|-|+-|-+|+-+|-|+-. */
long encode(long n, char *x);
/*
* Encode C-string x, up to n+1 chars. Return #encoded chars - 1
*/
long decode(long n, char *x);
/*
* Decode x, up to n+1 chars, to C-string. Return #decoded chars - 1
*/
/* .-|+-|-+|+-+|-|+-|-+|-|+-+|+-|-+|+-+|-|+-+|+-|-+|-|+-|-+|+-+|-|+-. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
/* .-|+-|-+|+-+|-|+-|-+|-|+-+|+-|-+|+-+|-|+-+|+-|-+|-|+-|-+|+-+|-|+-. */
void take(int b, int kod, const char *msg, int id)
{
if(!b
){ printf("%d:%s\n",id
,msg
); exit(kod
); } }
/* .-|+-|-+|+-+|-|+-|-+|-|+-+|+-|-+|+-+|-|+-+|+-|-+|-|+-|-+|+-+|-|+-. */
void fail(int b, int kod, const char *msg, int id)
{
if(b
) { printf("%d:%s\n",id
,msg
); exit(kod
); } }
/* .-|+-|-+|+-+|-|+-|-+|-|+-+|+-|-+|+-+|-|+-+|+-|-+|-|+-|-+|+-+|-|+-. */
#define FINPUT 1 /* Input failure */
#define FSAMPL 2 /* Non-suitable text sample */
#define FENCOD 3 /* Encode bug */
#define FDECOD 4 /* Decode bug */
#define FSECUR 5 /* Security bug */
#define FLOGIC 6 /* Other logical bug */
/* .-|+-|-+|+-+|-|+-|-+|-|+-+|+-|-+|+-+|-|+-+|+-|-+|-|+-|-+|+-+|-|+-. */
void test(int g, char *a, const char *b, long M, long L, long m)
/*
* encode-then-decode test, up to m+1 chars, on a C-string of length L
* already contained in memory section a of size M+1, whose initial
* content is identical to section b and remains identical upon return
* (if any). Report failure (and exit) using g as test case ID.
*/
{
fail
(memcmp(a
,b
,M
+1),FLOGIC
, "Logical bug: precondition #1.",g
); take(L < M, FLOGIC,"Logical bug: precondition #2.",g);
fail
(L
-strlen(a
),FLOGIC
,"Logical bug: precondition #3.",g
); take(m <=M, FLOGIC,"Logical bug: precondition #4.",g);
fail(m <-1, FLOGIC,"Logical bug: precondition #5.",g);
long n = L < m ? L : m;
long N = encode(m,a);
fail(N > n, FSECUR, "Encode bug: probably mem. violation.",g);
fail(N < n, FENCOD, "Bad encode: length mismatch.",g);
fail
(memcmp(a
+n
+1,b
+n
+1,M
-n
), FSECUR
, "Encode bug: mem. violation.",g
); for(long i=1;i<N;i+=1) take(a[i]-a[i-1],FENCOD, "Bad encode: twin.",g);
long K = decode(m,a);
fail(K > n, FSECUR, "Decode bug: probably mem. violation.",g);
fail(K < n, FDECOD, "Bad decode: length mismatch.",g);
fail
(memcmp(a
+n
+1,b
+n
+1,M
-n
), FSECUR
, "Decode bug: mem. violation.",g
); fail
(memcmp(a
,b
,K
+1), FDECOD
, "Bad decode: wrong original.",g
);}
/* .-|+-|-+|+-+|-|+-|-+|-|+-+|+-|-+|+-+|-|+-+|+-|-+|-|+-|-+|+-+|-|+-. */
void test2(int g, int n)
/*
* Do a random sequence of n encode-or-decode actions then the reverse
* sequence of counter-actions on a memory sector, which should revert
* to its original content when the sequences complete.
*
* Report failure using g as test case ID, and exit.
*/
{
long const MEM=256;
char x[MEM], y[MEM];
for(long i
=0;i
<MEM
;i
+=1) x
[i
]=y
[i
]=rand(); unsigned const a0=123456789, b0=987654321, a1=102505021, b1= -b0*a1;
fail(a0*a1-1, FLOGIC, "Bad reverse PRNG.",g);
long const m=176; /* Even point: Pr[strlen(x)<=m | x random] ~ 0.5 */
take(m<MEM, FLOGIC, "Logical bug: precondition #4.",g);
fail(m< -1, FLOGIC, "Logical bug: precondition #5.",g);
unsigned const msb=(unsigned)(-1)/2+1; /* MININT */
for(int k=n;k;k-=1){r = r*a0+b0; r&msb ? encode(m,x) : decode(m,x);}
for(int k=n;k;k-=1){r&msb ? decode(m,x) : encode(m,x); r = r*a1+b1;}
fail
(memcmp(x
,y
,MEM
), FSECUR
, "Bad algo: non-bijective coding.",g
);}
/* .-|+-|-+|+-+|-|+-|-+|-|+-+|+-|-+|+-+|-|+-+|+-|-+|-|+-|-+|+-+|-|+-. */
#define NUL '\0'
#define M ( (long)1E8 ) /* Max sample size incl. NUL terminator */
/* .-|+-|-+|+-+|-|+-|-+|-|+-+|+-|-+|+-+|-|+-+|+-|-+|-|+-|-+|+-+|-|+-. */
int main()
{
typedef long long IN;
static char a[M+1], b[M+1];
for(long i
=0;i
<sizeof(a
);i
+=1) a
[i
]=b
[i
]=rand(); char *t = a;
*t = NUL;
for(long n
=M
;(n
>1)*(IN
)fgets(t
,n
,stdin
);){long k
=strlen(t
);t
+=k
;n
-=k
;} fail(*t != NUL, FINPUT, "Input failure #1.", 0);
long L = t - a;
take(L < M, FINPUT, "Input failure #2.", 0);
fail
(L
-strlen(a
),FINPUT
, "Input failure #3.", 0); long nTwin = 0, nFlip = 0;
for(long i=0;i<L-1;i+=1){if(a[i]-a[i+1]) nFlip+=1; else nTwin+=1;}
take(nTwin, FSAMPL, "Bad sample: no twin symbols.", 0);
take(nFlip, FSAMPL, "Bad sample: no flip symbols.", 0);
test(1, a, b, M, L, M-1);
test(2, a, b, M, L, L+1);
test(3, a, b, M, L, L);
test(4, a, b, M, L, L-1);
test(5, a, b, M, L, L/2);
test(6, a, b, M, L, 1);
test(7, a, b, M, L, 0);
test(8, a, b, M, L,-1);
for(int n=(int)1E4;n;n-=1) test2(10,101);
printf( "%ld symbols encoded. You got %.0f points.\n", L
, log10(L
) ); return 0;
}
/*23456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEFGHIJKLMNOPQRSTUVWX*/
/*
* Put code here
*
* Define encode() and decode(), nothing else.
*/
#define VERSION 3
/*:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.*/
#if !(VERSION-0)
long encode(long n, char *x)
{
if( n<0 ) return n;
if(!x[0]) return 0;
long i;
for(i=1;(i<n+1)&(x[i]!=0);i+=1) if(!(x[i]-x[i-1]) ) x[i] = NUL;
if( i<n+1 ) x[i] = x[i-1];
return i<n+1 ? i : n;
}
/*:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.*/
long decode(long n, char *x)
{
if( n<0 ) return n;
if(!x[0]) return 0;
long i;
char c = x[0];
for(i=1;(i<n+1)&(x[i]!=c);i+=1) { c=x[i]; if(!x[i]) x[i] = x[i-1]; }
if( i<n+1 ) x[i] = NUL;
return i<n+1 ? i : n;
}
/*:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.*/
#elif !(VERSION-1)
long encode(long n, char *x)
{
long s = 0;
for(long i=0;i<n+1;i+=1){ x[i]+=s; if( !(x[i]-s) ) return i; s=x[i]; }
return n;
}
/*:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.*/
long decode(long n, char *x)
{
long s = 0;
for(long i=0;i<n+1;i+=1){ x[i]-=s; s+=x[i]; if(!x[i]) return i; }
return n;
}
/*:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.*/
#elif !(VERSION-2)
#define DEL '\x7F'
long encode(long n, char *x)
{
if( n<0 ) return n;
char const R = DEL;
if(!( (x[0]!=NUL)&(x[0]!=R) ) ) return 0;
long i;
for(i=1;(i<n+1)&(x[i]!=0)&(x[i]!=R);i+=1) if(!(x[i]-x[i-1]) ) x[i]=R;
if( (i<n+1)&!(x[i]-R) ) x[i] = x[i-1];
return i<n+1 ? i : n;
}
/*:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.*/
long decode(long n, char *x)
{
if( n<0 ) return n;
char const R = DEL;
if(!( (x[0]!=NUL)&(x[0]!=R) ) ) return 0;
long i;
char c = x[0];
for(i=1;(i<n+1)&(x[i]!=0)&(x[i]!=c);i+=1)
{ c=x[i]; if(!(x[i]-R) )x[i] = x[i-1]; }
if( (i<n+1)&!(x[i]-c) ) x[i] = R;
return i<n+1 ? i : n;
}
/*:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.*/
#elif !(VERSION-3)
#define SUB '\x1A'
#define ESC '\x1B'
#define DEL '\x7F'
typedef unsigned char ucha;
/*:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.*/
long encode(long n, char *scx)
{
long n;
ucha const m = -1, e = DEL;
ucha s = e, * const x =(ucha*)scx;
long i;
for(i=0;(i<n+1)&(x[i]!=0);i+=1)
{ ucha t=x[i]; x[i]=(t+s-e-1+m)%m+1; if(!(t-e) ) return i; s=x[i]; }
return i<n+1 ? i : n;
}
/*:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.*/
long decode(long n, char *scx)
{
ucha const m = -1, e = DEL;
ucha s = e, * const x =(ucha*)scx;
long i;
for(i=0;(i<n+1)&(x[i]!=0);i+=1)
{ ucha t=x[i]; x[i]=(t-s+e-1+m)%m+1; if(!(t-s) )return i; s=t;}
return i<n+1 ? i : n;
}
#endif
/* DIPHUSENHOPEBESTCONFVZENTHROWDYCENSAZSADATLAZTQOZSKIPJAXDAMNFRYGHT */