// encode string using lz78: #include #include #include #include #include #include #include using namespace std; template class vechasher: public std::unary_function,std::size_t> { public: std::size_t operator()(const vector &v) const { std::size_t ret = 0; for(auto &x : v) ret += std::hash()(x); return ret; } }; template ostream &operator<<(ostream &os, const vector &v) { copy(begin(v),end(v),ostream_iterator(os,",")); return os; } template ostream &operator<<(ostream &os, const std::pair &p) { os << p.first << "->" << p.second; return os; } template auto encode(const T &s) -> vector::type>::type>> { typedef typename remove_cv< typename remove_reference< decltype(*begin(s))>::type>::type unittype; unordered_map,int,vechasher> dict; vector> ret; int foundnum = 0,maxnum = 0; vector word; for(auto x : s) { word.emplace_back(x); auto loc = dict.find(word); if (loc == dict.end()) { ret.emplace_back(foundnum,x); //dict.emplace(word,++maxnum); dict[word] = ++maxnum; foundnum = 0; word.resize(0); } else foundnum = loc->second; } if (foundnum!=0) { auto &x = word.back(); word.pop_back(); auto loc = dict.find(word); ret.emplace_back(loc==dict.end() ? 0 : loc->second,x); } cout << "hash table useage: " << endl; auto nbuck = dict.bucket_count(); for(size_t b = 0;bfirst << "->" << x->second; cout << endl; } return ret; } int main(int argc, char **argv) { string s; cin >> s; auto encoding = encode(s); for(auto &x : encoding) cout << "(" << x.first << ',' << x.second << ") "; cout << endl; }