-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHuffmanDecoding.cpp
More file actions
162 lines (143 loc) · 3.83 KB
/
HuffmanDecoding.cpp
File metadata and controls
162 lines (143 loc) · 3.83 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <cstdio>
#include <string>
#include <cctype>
#include <ios>
#include <iostream>
#include <numeric>
#include <vector>
#include <list>
#include <cmath>
#include <algorithm>
#include <cstdlib> // malloc
#include <climits>
#include <utility> // pair
#include <cstring>
#include <ctime>
#include <set>
#include <sstream>
#include <typeinfo>
#include <fstream>
#include <iomanip>
using namespace std;
#define DEBUG(x) cout << #x << ": " << x << "n"
class HuffmanDecoding {
public:
string decode(string archive, vector<string> d) {
cout << endl;
string ret="";
int b=0;
while(b<archive.size()) {
for(int len=1; (b+len)<=archive.size(); ++len) {
string ss = archive.substr(b, len);
vector<string>::iterator iter = find(d.begin(), d.end(), ss);
if(iter!=d.end()) {
int dpos = iter-d.begin();
char c = 'A'+dpos;
// cout << c << endl;
b = b+len-1;
ret = ret+c;
break; // go to new 'b' while loop
}
}
++b;
}
return ret;
}
};
// CUT begin
ifstream data("HuffmanDecoding.sample");
string next_line() {
string s;
getline(data, s);
return s;
}
template <typename T> void from_stream(T &t) {
stringstream ss(next_line());
ss >> t;
}
void from_stream(string &s) {
s = next_line();
}
template <typename T> void from_stream(vector<T> &ts) {
int len;
from_stream(len);
ts.clear();
for (int i = 0; i < len; ++i) {
T t;
from_stream(t);
ts.push_back(t);
}
}
template <typename T>
string to_string(T t) {
stringstream s;
s << t;
return s.str();
}
string to_string(string t) {
return "\"" + t + "\"";
}
bool do_test(string archive, vector<string> dictionary, string __expected) {
time_t startClock = clock();
HuffmanDecoding *instance = new HuffmanDecoding();
string __result = instance->decode(archive, dictionary);
double elapsed = (double)(clock() - startClock) / CLOCKS_PER_SEC;
delete instance;
if (__result == __expected) {
cout << "PASSED!" << " (" << elapsed << " seconds)" << endl;
return true;
}
else {
cout << "FAILED!" << " (" << elapsed << " seconds)" << endl;
cout << " Expected: " << to_string(__expected) << endl;
cout << " Received: " << to_string(__result) << endl;
return false;
}
}
int run_test(bool mainProcess, const set<int> &case_set, const string command) {
int cases = 0, passed = 0;
while (true) {
if (next_line().find("--") != 0)
break;
string archive;
from_stream(archive);
vector<string> dictionary;
from_stream(dictionary);
next_line();
string __answer;
from_stream(__answer);
cases++;
if (case_set.size() > 0 && case_set.find(cases - 1) == case_set.end())
continue;
cout << " Testcase #" << cases - 1 << " ... ";
if ( do_test(archive, dictionary, __answer)) {
passed++;
}
}
if (mainProcess) {
cout << endl << "Passed : " << passed << "/" << cases << " cases" << endl;
int T = time(NULL) - 1484304614;
double PT = T / 60.0, TT = 75.0;
cout << "Time : " << T / 60 << " minutes " << T % 60 << " secs" << endl;
cout << "Score : " << 450 * (0.3 + (0.7 * TT * TT) / (10.0 * PT * PT + TT * TT)) << " points" << endl;
}
return 0;
}
int main(int argc, char *argv[]) {
cout.setf(ios::fixed, ios::floatfield);
cout.precision(2);
set<int> cases;
bool mainProcess = true;
for (int i = 1; i < argc; ++i) {
if ( string(argv[i]) == "-") {
mainProcess = false;
} else {
cases.insert(atoi(argv[i]));
}
}
if (mainProcess) {
cout << "HuffmanDecoding (450 Points)" << endl << endl;
}
return run_test(mainProcess, cases, argv[0]);
}
// CUT end