-
Notifications
You must be signed in to change notification settings - Fork 0
/
fencode.cpp
107 lines (95 loc) · 2.6 KB
/
fencode.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
102
103
104
105
106
107
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <set>
using namespace std;
vector<int> encode(const string &s);
vector<int> encode(const vector<int> &s);
vector<int> encode(vector<int>::iterator start, vector<int>::iterator end);
void index(const string &path, set<string> &dict);
int main() {
/*
set<string> dict;
index("english_words.txt", dict);
map<vector<int>, vector<string> > encodings;
for(auto i = dict.begin(); i != dict.end(); i++) {
vector<int> e = encode(*i);
encodings[e].push_back(*i);
}
vector<int> ct_encode = encode(cipher_text.begin(), cipher_text.end());
for(auto i = ct_encode.begin(); i != ct_encode.end(); i++)
cout << *i << ',';
cout << endl;
vector<string> result = encodings[ct_encode];
for(auto i = result.begin(); i != result.end(); i++) {
cout << *i << endl;
}
/*
for(auto i = encodings.begin(), j = next(i, 10); i != j; i++) {
for(int k : i->first) cout << k << " ";
cout << ": ";
for(string s : i->second) cout << s << ", ";
cout << endl;
}
*/
return 0;
}
vector<int> encode(const string &s)
{
// seen keeps track of letters we saw already and what we mapped them to
// 0..25 maps to a..z
vector<int> encoding;
vector<int> seen(26, -1);
int mapping = 0;
for(int i = 0; i < s.size(); i++) {
if(seen[s[i]-'a'] < 0) {
seen[s[i]-'a'] = mapping;
encoding.push_back(mapping);
mapping++;
}
else encoding.push_back(seen[s[i]-'a']);
}
return encoding;
}
vector<int> encode(const vector<int> &s)
{
vector<int> encoding;
vector<int> seen(103, -1);
int mapping = 0;
for(int i = 0; i < s.size(); i++) {
if(seen[s[i]] < 0) {
seen[s[i]] = mapping;
encoding.push_back(mapping);
mapping++;
}
else encoding.push_back(seen[s[i]]);
}
return encoding;
}
vector<int> encode(vector<int>::iterator start, vector<int>::iterator end)
{
vector<int> encoding;
vector<int> seen(103, -1);
int mapping = 0;
for(auto i = start; i != end; i++) {
if(seen[*i] < 0) {
seen[*i] = mapping;
encoding.push_back(mapping);
mapping++;
}
else encoding.push_back(seen[*i]);
}
return encoding;
}
void index(const string &path, set<string> &dict)
{
ifstream ifs(path);
if(!ifs) {
cerr << "Error opening " << path << endl;
exit(1);
}
string line;
while(getline(ifs, line)) dict.insert(line);
}