-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlzw.cpp
More file actions
55 lines (49 loc) · 1.15 KB
/
lzw.cpp
File metadata and controls
55 lines (49 loc) · 1.15 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
#include<bits/stdc++.h>
using namespace std;
int main(){
string s1;
cout<<"\nenter the string :";
cin>>s1;
//ENCODER MAP
unordered_map<string,int> mp;
//DECODER MAP
unordered_map<int,string> dec;
int j = 256;
string p ="";
int i = 0,l = 0;
vector<int> op;
while(l != s1.length()){
p = s1.substr(i,l-i+1);
if(mp.find(p)==mp.end()){
if(p.length() == 1){
mp[p]=p[0];
dec[p[0]] = p;
}
else{
mp[p]=j;
dec[j] = p;
j++;
}
cout<<p<<":"<<mp[p]<<"\n";
op.push_back(mp[p]);
i = l+1;
l = l+1;
}
else{
if(l == s1.length()-1){
op.push_back(mp[s1.substr(i,l-i+1)]);
}
l++;
}
}
cout<<endl;
for(int i = 0;i < op.size();i++){
cout<<op[i];
}
cout<<endl;
//DECODING...........................
for(int i = 0;i < op.size();i++){
cout<<dec[op[i]];
}
cout<<endl;
}