forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayfair.cpp
101 lines (91 loc) · 2.05 KB
/
playfair.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <bits/stdc++.h>
using namespace std;
int main() {
string cipher;
string message;
getline(cin, cipher);
getline(cin, message);
map<char,pair<int,int>> m;
map<pair<int,int>,char> rev;
int x = 0;
int y = 0;
for(auto i : cipher) {
if(i == ' ') {
continue;
}
if(m.count(i) == 0) {
rev[{x,y}] = i;
m[i] = {x,y};
y++;
if(y >= 5) {
x++;
y = 0;
}
}
}
for(char i = 'a'; i <= 'z'; i++) {
if(i == 'q') {
continue;
}
if(m.count(i) == 0) {
rev[{x,y}] = i;
m[i] = {x,y};
y++;
if(y >= 5) {
x++;
y = 0;
}
}
}
string nospaces;
for(auto i : message) {
if(i != ' ') {
nospaces.push_back(i);
}
}
string actual;
int idx = 0;
for(int i = 0; i < nospaces.size(); i++) {
if(idx%2==1 && actual[idx-1] == nospaces[i]) {
actual.push_back('x');
idx++;
}
actual.push_back(nospaces[i]);
idx++;
}
string answer;
int n = actual.size();
for(int i = 0; i < n; i+=2) {
char c1 = actual[i];
char c2;
if(i == n-1) {
c2 = 'x';
}
else {
c2 = actual[i+1];
}
if(c1 == c2) {
c2 = 'x';
}
int x1 = m[c1].first;
int y1 = m[c1].second;
int x2 = m[c2].first;
int y2 = m[c2].second;
if(m[c1].first == m[c2].first) {
answer += rev[{x1, (y1+1)%5}];
answer += rev[{x2, (y2+1)%5}];
}
else if(m[c1].second == m[c2].second) {
answer += rev[{(x1+1)%5, y1}];
answer += rev[{(x2+1)%5, y2}];
}
else {
answer += rev[{x1,y2}];
answer += rev[{x2,y1}];
}
}
for(auto i : answer) {
cout << (char)toupper(i);
}
cout << endl;
}