-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecrypt.cpp
More file actions
76 lines (66 loc) · 2.12 KB
/
decrypt.cpp
File metadata and controls
76 lines (66 loc) · 2.12 KB
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
#include<bits/stdc++.h>
using namespace std;
struct Node {
char c;
bool is_char;
Node *left, *right;
Node() : left(nullptr), right(nullptr), is_char(false) {}
void add_code(const char& ch, const string& code, int iter = 0) {
if(iter == code.size()) {
c = ch;
is_char = true;
} else {
if(code[iter] == '0') {
if(left == nullptr) left = new Node();
left->add_code(ch, code, iter+1);
}
else {
if(right == nullptr) right = new Node();
right->add_code(ch, code, iter+1);
}
}
}
pair<char, int> find_char(const string& message, int iter) {
if(is_char) {
return {c, iter};
} else {
if(message[iter] == '0') {
if(left == nullptr) {
cout << "The message or key has been given uncorrectly. Please try again later.";
exit(0);
}
return left->find_char(message, iter+1); cerr << "Going left!\n";
}else {
if(right == nullptr) {
cout << "The message or key has been given uncorrectly. Please try again later.";
exit(0);
}
return right->find_char(message, iter+1); cerr << "Going right!\n";
}
}
}
};
int main() {
Node *root = new Node();
string encrypted_message;
cout << "Enter the encrypted message first:" << endl;
cin >> encrypted_message;
cout << "Enter the required key with the suitable form please:" << endl;
int data_num;
cin >> data_num;
for(int i = 0; i < data_num; i++) {
int ch;
string code;
cin >> ch >> code;
root->add_code((char)ch, code);
}
string decrypted_message;
int idx = 0;
while(idx < encrypted_message.length()) {
auto p = root->find_char(encrypted_message, idx);
decrypted_message += p.first;
idx = p.second;
}
cout << "Decrypted message is:" << endl << decrypted_message;
return 0;
}