forked from Otrebus/timus
/
1236.cpp
42 lines (37 loc) · 1.05 KB
/
1236.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/* 1236. Decoding Task - http://acm.timus.ru/problem.aspx?num=1236
*
* Strategy:
* The given space character xored with the first byte of the result yields the first byte of the
* key. Then, the first byte of the key xored with the first byte of the first string gives us the
* decoded second byte of the second string, which we can xor with the coded second byte of the
* second string to give us the second byte of the key, and so on.
*
* Performance:
* O(N), runs in 0.015s using 240KB memory.
*/
#include <stdio.h>
char a[20001];
char b[20003];
unsigned char hexToChar(char c)
{
return c >= '0' && c <= '9' ? c - '0' : 10 + c - 'A';
}
unsigned char getHex(char* a)
{
return 16*(hexToChar(a[0])) + hexToChar(a[1]);
}
int main()
{
int N;
scanf("%s%n", a, &N);
scanf("%s", b);
unsigned char p = 32;
unsigned char k = p^getHex(b);
printf("%02X", (unsigned char) k);
for(int i = 0; i < N; i+=2)
{
unsigned char d = k^getHex(a+i);
k = d^getHex(b+i+2);
printf("%02X", (unsigned char) k);
}
}