当前位置:网站首页>Huffman coding
Huffman coding
2022-06-29 12:34:00 【The world is ordinary】
Optimized using priority queues haffman code .
- Count the word frequency in the article
- Use priority queues ( Pile up ) establish haffman Trees
- Traverse Haffman The tree gets the of each character huffman code , Save in enc(unordered_map) in
- Code the article
- Use huffman Trees and haffman Code to decode .
establish huffman The time complexity of trees O ( C l o g C ) O(ClogC) O(ClogC) among C Is the number of word types .
The time complexity of coding O ( n ) O(n) O(n), You just need to iterate through the string .
The time complexity of decoding O ( m ) O(m) O(m), Just traverse the encoded string once .
huffman Coding is the basis of modern compression technology . I should write an I / O to test the compression of the file .
char yes 8 individual bit, and bit One bit in the array is 1 individual bit. So there must be compression effect .
The last file saved is haffman Tree and corresponding huaffman code . Read first when decoding haffmans Trees , Then decode the binary file .
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <unordered_map>
using namespace std;
// Encodes a URL to a shortened URL.
struct HuffmanTree {
HuffmanTree *l, *r;
int cnt;
char ch; // Word frequency and words
HuffmanTree(int cnt, char ch, HuffmanTree *l, HuffmanTree *r):cnt(cnt), ch(ch), l(l), r(r) {}
};
class cmp{
public:
/* Node::priority Big priority , Override less than sign */
bool operator () (HuffmanTree* &a, HuffmanTree* &b) const
{
return a->cnt > b->cnt;
}
};
typedef pair<int, char>PR;
priority_queue<HuffmanTree*, vector<HuffmanTree*>, cmp>que, bakcup;
unordered_map<char, int>mp;
HuffmanTree* buildHuffman() {
bakcup = que;
while (!bakcup.empty()) {
printf("%d %c\n", bakcup.top()->cnt, bakcup.top()->ch);
bakcup.pop();
}
while (que.size() >= 2) {
HuffmanTree* t1 = que.top(); que.pop();
HuffmanTree* t2 = que.top(); que.pop();
HuffmanTree *fa = new HuffmanTree(t1->cnt + t2->cnt, '#', t1, t2);
que.push(fa);
}
return que.top();
}
HuffmanTree *root;
unordered_map<char, string>enc;
void travel(HuffmanTree *root, string s) { // Get the of each node huffman code
if (root == NULL) return ;
// leaf node , Statistical coding
if (root->l == NULL && root->r == NULL) {
cout << root->ch << " " << s << endl;
enc[root->ch] = s;
return ;
}
travel(root->l, s + '0');
travel(root->r, s + '1');
}
string encode(string longUrl) {
for (char ch : longUrl) {
mp[ch]++;
}
for (auto [ch, cnt] : mp) {
que.push(new HuffmanTree(cnt, ch, NULL, NULL)); // It's storing the hands .
}
root = buildHuffman();
travel(root, ""); // Traverse haffman Tree acquisition Huffman code
string ans;
for (char ch : longUrl) {
ans += enc[ch];
}
return ans;
}
string treeDecode(HuffmanTree *root, int &cur, string &s) {
if (root == NULL) return "";
if (root->l == NULL && root->r == NULL) {
string ans = "";
ans += root->ch;
return ans;
}
cur++;
if (s[cur] == '0') return treeDecode(root->l, cur, s);
else return treeDecode(root->r, cur, s);
}
// Decodes a shortened URL to its original URL.
string decode(string shortUrl) {
int cur = -1;
string ans;
int n = shortUrl.size();
for (cur; cur < n - 1;) {
printf("cur = %d\n", cur);
string s = treeDecode(root, cur, shortUrl);
cout << s << endl;
ans += s;
}
return ans;
}
int main() {
string s = "aaabbc";
s = encode(s);
cout << s << endl;
cout << decode(s) << endl;
}
边栏推荐
猜你喜欢

MySQL 主从复制原理以及流程

Kyligence Zen, an intelligent indicator driven management and decision-making platform, is newly launched and is in limited internal testing

Matlab GUI realizes the function of clicking the button, opening the file dialog box and importing pictures

Understanding of P value
![[leetcode] 14. Longest public prefix](/img/3b/3388ce8382ad5caaaf0a42488da2f9.png)
[leetcode] 14. Longest public prefix

地球观测卫星数据

The blackened honeysnow ice city wants to grasp the hearts of consumers by marketing?

论文复现——AC-FPN:Attention-guided Context Feature Pyramid Network for Object Detection.

面试突击61:说一下MySQL事务隔离级别?

参加2022年杭州站Cocos Star Meetings
随机推荐
GBase8s数据库select有HAVING 子句
How to fix ora-01017: invalid user name / password login denied
Titanium dynamic technology: our Zadig landing Road
Gbase8s database select has order by Clause 3
GBase8s数据库select有ORDER BY 子句6
Zhengda futures liu4 data integration
GBase8s数据库select有ORDER BY 子句3
nacos启动报错
Uncover the practice of Baidu intelligent test in the field of automatic test execution
GBase 8s 扩展外连接1
Cmake error
Gbase8s database for update clause
How to install oracle19c in Centos8
NvtBack
bison使用error死循环的记录
Gbase8s database select has order by Clause 5
对p值的理解
MySQL数据库主从同步,一致性解决方案
An interpretable geometric depth learning model for structure based protein binding site prediction
535. encryption and decryption of tinyurl: design a URL simplification system