-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhelpers.h
More file actions
53 lines (44 loc) · 1.14 KB
/
helpers.h
File metadata and controls
53 lines (44 loc) · 1.14 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
//
// Created by Rahul Govind on 15/12/18.
//
#ifndef PROJECT_HELPERS_H
#define PROJECT_HELPERS_H
#include <bits/stdc++.h>
using namespace std;
class Encoder {
private:
map<char, char> e;
map<char, char> d;
public:
Encoder(string s) {
char present[256];
for (int i = 0; i < 256; i++) { present[i] = false; }
for (int i = 0; i < s.length(); i++) { present[s[i]] = true; }
int count = 0;
for (int i = 0; i < 256; i++) {
if (present[i]) {
e[(char) i] = (char) ('0' + count);
d[(char) ('0' + count)] = (char) i;
count++;
}
}
}
size_t charset_size() {
return e.size();
}
string encode(string s) {
vector<char> v;
for (int i = 0; i < s.length(); i++) {
v.push_back(e[s[i]]);
}
return string(v.begin(), v.end());
}
string decode(string s) {
vector<char> v;
for (int i = 0; i < s.length(); i++) {
v.push_back(s[i] == '-' ? '-' : d[s[i]]);
}
return string(v.begin(), v.end());
}
};
#endif //PROJECT_HELPERS_H