-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoding.cpp
64 lines (56 loc) · 1.5 KB
/
decoding.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
#include <bits/stdc++.h>
using namespace std;
int decode(string binaryString){
int value = 0;
int indexCounter = 0;
for(int i=binaryString.length()-1;i>=0;i--){
if(binaryString[i]=='1'){
value += pow(2, indexCounter);
}
indexCounter++;
}
return value;
}
int main(){
string dictionary,str,decoded_str;
time_t start, end;
time(&start);
// ifstream info("output.txt");
// int length = 5000000; // num of chars you want to read
// str.resize(length, ' '); // reserve spaces
// char* begin = &*str.begin();
// info.read(begin, length);
// cout<<str.length()<<endl;
ifstream outfile;
outfile.open("output.txt");
outfile>>str;
ifstream indictfile;
indictfile.open("dictionary.txt");
indictfile>>dictionary;
int len=0,dist,l;
for(int i=0;i<str.length();){
while(str[i]=='0'){
len++;
i++;
}
dist=10000-decode(str.substr(i,len))-1;
i+=len,len=0;
while(str[i]=='0'){
len++;
i++;
}
l=decode(str.substr(i,len));
i+=len;
len=0;
decoded_str+=dictionary.substr(dist,l);
}
std::ofstream outputfile;
outputfile.open("decoded.txt", std::ios_base::app); // append instead of overwrite
outputfile << decoded_str;
outputfile.close();
time(&end);
double time_taken = double(end - start);
cout << "Time taken by program to decode is : " << fixed
<< time_taken << setprecision(5);
cout << " sec " << endl;
}