-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
44 lines (39 loc) · 1.02 KB
/
main.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
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
int k, n;
char ciphertext[128];
int ciphercode[128];
int plaincode[128];
char plaintext[128];
while (scanf("%d", &k) && k != 0)
{
scanf("%s", ciphertext);
n = strlen(ciphertext);
for (int i = 0; i < n; i++)
{
if (ciphertext[i] == '_')
ciphercode[i] = 0;
else if (ciphertext[i] == '.')
ciphercode[i] = 27;
else
ciphercode[i] = ciphertext[i] - 'a' + 1;
}
for (int i = 0; i < n; i++)
plaincode[(k * i) % n] = (ciphercode[i] + i) % 28;
for (int i = 0; i < n; i++)
{
if (plaincode[i] == 0)
plaintext[i] = '_';
else if (plaincode[i] == 27)
plaintext[i] = '.';
else
plaintext[i] = plaincode[i] + 'a' - 1;
}
plaintext[n] = '\0';
printf("%s\n", plaintext);
}
return 0;
}