Skip to content

Instantly share code, notes, and snippets.

@jacky860226
Created June 14, 2019 12:18
Show Gist options
  • Save jacky860226/c436c68beb07782ba91306544a392d9e to your computer and use it in GitHub Desktop.
Save jacky860226/c436c68beb07782ba91306544a392d9e to your computer and use it in GitHub Desktop.
Lempel–Ziv–Welch (LZW) c++ implement
#include<vector>
#include<string>
#include<map>
using namespace std;
string decode(const vector<int> &v){
map<int,string> inv_dict;
int dictSize = 256;
for(int i=0;i<dictSize;++i)
inv_dict[i] = string(1,i);
string s, entry, res;
s = res = inv_dict[v[0]];
for(size_t i = 1; i<v.size(); ++i){
int k = v[i];
if(inv_dict.count(k))
entry = inv_dict[k];
else if(k==dictSize)
entry = s+s[0];
else throw "error";
res += entry;
inv_dict[dictSize++] = s + entry[0];
s = entry;
}
return res;
}
#include<vector>
#include<string>
#include<map>
using namespace std;
vector<int> encode(const string &ori){
map<string,int> dict;
int dictSize = 256;
for(int i=0;i<dictSize;++i)
dict[string(1,i)] = i;
vector<int> res;
string s;
for(char z : ori){
if(dict.count(s+z)) s += z;
else{
res.emplace_back(dict[s]);
dict[s+z] = dictSize++;
s = z;
}
}
if(s.size()) res.emplace_back(dict[s]);
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment