-
Notifications
You must be signed in to change notification settings - Fork 0
/
641 - Do the Untwist.cpp
72 lines (56 loc) · 1.36 KB
/
641 - Do the Untwist.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <array>
#include <cstring>
#define LENGTH 70
using namespace std;
int encode(char c);
char decode(int n);
int main() {
array<char, LENGTH> ciphertext = {0};
array<int, LENGTH> ciphercode = {0};
array<char, LENGTH> plaintext = {0};
int k;
cin >> k;
while (k != 0) {
cin >> ciphertext.data();
int n = strlen(ciphertext.data());
for (int i = 0; i < n; i++) {
ciphercode[i] = encode(ciphertext[i]);
}
for (int i = 0; i < n; i++) {
int plain_pos = (k * i) % n;
for (int plain = 0; ; plain++) {
int x = ((plain - i) / 28) * 28 + ciphercode[i];
int y = plain - i;
if (x == y) {
plaintext[plain_pos] = decode(plain % 28);
break;
}
}
}
cout << plaintext.data() << endl;
ciphertext.fill(0);
ciphercode.fill(0);
plaintext.fill(0);
cin >> k;
}
return 0;
}
int encode(char c) {
if (c == '_') {
return 0;
} else if (c >= 'a' && c <= 'z') {
return c - 'a' + 1;
} else {
return 27;
}
}
char decode(int n) {
if (n == 0) {
return '_';
} else if (n >= 1 && n <= 26) {
return 'a' + n - 1;
} else {
return '.';
}
}