当前位置:网站首页>Question d: Haffman coding
Question d: Haffman coding
2022-07-04 01:58:00 【Wait for dawn forever】
Title Description
Huffman code must be familiar to you ( It doesn't matter if I'm not familiar with it , Find out for yourself ...). Now I give you a string of characters and their corresponding weights , Let you construct Huffman tree , So as to determine the Huffman code of each character . Of course , Here are some small rules :
1. The left subtree of Huffman tree is coded as 0, The right subtree is coded as 1;
2. If the weight of two characters is the same , be ASCII Characters with small code value are left children , The big one is the right child ;
3. The characters represented by the created new node are the same as those of its child ;
4. All characters are ASCII On the code table 32-96 Characters between ( namely “ ” To “`” Characters between ).
Input
The input contains multiple sets of data ( No more than 100 Group )
The first row of each group of data is an integer n, Represents the number of characters . Next n That's ok , Each line has one character ch And an integer weight, According to the character ch The corresponding weight , Space separates the middle .
Input data to ensure that the characters of each group of test data will not be repeated .
Output
For each group of test data , Output the corresponding characters and their Huffman coding results in the input order , See the example for the specific format .
The sample input
3
a 10
b 5
c 8
4
a 1
b 1
c 1
d 1
Sample output
a:0
b:10
c:11
a:00
b:01
c:10
d:11
Ideas : Top down Huffman coding .
#include <cstdio>
#include <climits>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 110;
struct chardata {
char ch;
int weight;
} w[maxn];
struct HuffmanNode {
int weight;
int parent, lchild, rchild;
} Node[maxn * 2];
void searchMin(int &a, int &b, int n) {
int min = INT_MAX;
// Find the decimal number a
for (int i = 1; i <= n; i++) {
if (Node[i].parent == 0 && Node[i].weight < min) {
min = Node[i].weight;
a = i;
}
}
min = INT_MAX;
// Find the sequence number of the next decimal b
for (int i = 1; i <= n; i++) {
if (Node[i].parent == 0 && Node[i].weight < min && i != a) {
min = Node[i].weight;
b = i;
}
}
// Guarantee serial number a Less than serial number b
if (a > b) {
swap(a, b);
}
}
void HuffmanCode(int n, chardata *w, char **&ans) {
int m = 2 * n - 1;
// Initialize original n Leaf node
for (int i = 1; i <= n; i++) {
Node[i].parent = Node[i].lchild = Node[i].rchild = 0;
Node[i].weight = w[i].weight;
}
// structure Huffman Binary tree
for (int i = n + 1; i <= m; i++) {
int a, b;
searchMin(a, b, i - 1);
Node[i].lchild = a;
Node[i].rchild = b;
Node[i].weight = Node[a].weight + Node[b].weight;
Node[i].parent = 0;
Node[a].parent = Node[b].parent = i; // Mark parent node
}
int c, f, len;
char temp[n];
ans = new char *[n + 1];
for (int i = 1; i <= n; i++) {
c = i;
len = n;
temp[len] = 0;
// Bottom up
while (Node[c].parent != 0) {
// Up to the root node
f = Node[c].parent; // Parent node
if (Node[f].lchild == c) {
// Left 0
temp[--len] = '0';
} else {
// Right 1
temp[--len] = '1';
}
c = f; // Keep going up
}
ans[i] = new char[n - len + 1];
strcpy(ans[i], temp + len); // Copied to the ans
}
}
int main() {
int n;
char **ans;
while (scanf("%d", &n) != EOF) {
for (int i = 1; i <= n; i++) {
getchar();
scanf("%c %d", &w[i].ch, &w[i].weight);
}
HuffmanCode(n, w, ans);
for (int i = 1; i <= n; i++) {
printf("%c:%s\n", w[i].ch, ans[i]);
}
}
delete ans;
return 0;
}
边栏推荐
- 2022 R2 mobile pressure vessel filling certificate examination and R2 mobile pressure vessel filling simulation examination questions
- Yyds dry goods inventory override and virtual of classes in C
- I don't know why it can't run in the project and how to change it
- LeetCode 168. Detailed explanation of Excel list name
- Applet graduation project is based on wechat classroom laboratory reservation applet graduation project opening report function reference
- Portapack application development tutorial (XVII) nRF24L01 launch C
- ES6 deletes an attribute in all array objects through map, deconstruction and extension operators
- Meta metauniverse female safety problems occur frequently, how to solve the relevant problems in the metauniverse?
- Install the pit that the electron has stepped on
- Will the memory of ParticleSystem be affected by maxparticles
猜你喜欢
What is the student party's Bluetooth headset recommendation? Student party easy to use Bluetooth headset recommended
The automatic control system of pump station has powerful functions and diverse application scenarios
Meta metauniverse female safety problems occur frequently, how to solve the relevant problems in the metauniverse?
Force buckle day32
The reasons why QT fails to connect to the database and common solutions
Jerry's synchronous weather information to equipment [chapter]
Life cycle of instance variables, static variables and local variables
Install the pit that the electron has stepped on
Solution to the problem that jsp language cannot be recognized in idea
Three layer switching ①
随机推荐
Ceramic metal crowns - current market situation and future development trend
MySQL introduction - functions (various function statistics, exercises, details, tables)
From the 18th line to the first line, the new story of the network security industry
Life cycle of instance variables, static variables and local variables
IPv6 experiment
Pyinstaller packaging py script warning:lib not found and other related issues
Example 073 square sum value judgment programming requires the input of a and B, if a ²+ b ² If the result of is greater than 100, a is output ²+ b ² Value, otherwise output the result of a + B.
Introduction to Tianchi news recommendation: 4 Characteristic Engineering
[leetcode daily question] a single element in an ordered array
Sequence sorting of basic exercises of test questions
How to delete MySQL components using xshell7?
Applet graduation project based on wechat selection voting applet graduation project opening report function reference
Functions and arrays of shell scripts
Hash table, string hash (special KMP)
Winter vacation daily question -- a single element in an ordered array
String & memory function (detailed explanation)
C import Xls data method summary II (save the uploaded file to the DataTable instance object)
How to view the computing power of GPU?
The automatic control system of pump station has powerful functions and diverse application scenarios
SRCNN:Learning a Deep Convolutional Network for Image Super-Resolution