7-1 哈夫曼编码
分数 300
全屏浏览题目
切换布局
作者 陈越
单位 浙江大学
给定一段文字,如果我们统计出字母出现的频率,是可以根据哈夫曼算法给出一套编码,使得用此编码压缩原文可以得到最短的编码总长。然而哈夫曼编码并不是唯一的。例如对字符串"aaaxuaxz",容易得到字母 'a'、'x'、'u'、'z' 的出现频率对应为 4、2、1、1。我们可以设计编码 {'a'=0, 'x'=10, 'u'=110, 'z'=111},也可以用另一套 {'a'=1, 'x'=01, 'u'=001, 'z'=000},还可以用 {'a'=0, 'x'=11, 'u'=100, 'z'=101},三套编码都可以把原文压缩到 14 个字节。但是 {'a'=0, 'x'=01, 'u'=011, 'z'=001} 就不是哈夫曼编码,因为用这套编码压缩得到 00001011001001 后,解码的结果不唯一,"aaaxuaxz" 和 "aazuaxax" 都可以对应解码的结果。本题就请你判断任一套编码是否哈夫曼编码。
输入格式:
首先第一行给出一个正整数 N(2≤N≤63),随后第二行给出 N 个不重复的字符及其出现频率,格式如下:
c[1] f[1] c[2] f[2] ... c[N] f[N]
其中c[i]
是集合{'0' - '9', 'a' - 'z', 'A' - 'Z', '_'}中的字符;f[i]
是c[i]
的出现频率,为不超过 1000 的整数。再下一行给出一个正整数 M(≤1000),随后是 M 套待检的编码。每套编码占 N 行,格式为:
c[i] code[i]
其中c[i]
是第i
个字符;code[i]
是不超过63个'0'和'1'的非空字符串。
输出格式:
对每套待检编码,如果是正确的哈夫曼编码,就在一行中输出"Yes",否则输出"No"。
注意:最优编码并不一定通过哈夫曼算法得到。任何能压缩到最优长度的前缀编码都应被判为正确。
输入样例:
7
A 1 B 1 C 1 D 3 E 3 F 6 G 6
4
A 00000
B 00001
C 0001
D 001
E 01
F 10
G 11
A 01010
B 01011
C 0100
D 011
E 10
F 11
G 00
A 000
B 001
C 010
D 011
E 100
F 101
G 110
A 00000
B 00001
C 0001
D 001
E 00
F 10
G 11
输出样例:
Yes
Yes
No
No
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
这题一开始我用了java来写,先附上java的代码:
import java.util.ArrayList;
import java.util.Scanner;
import static java.lang.System.exit;
class HufmTree{
private int weight;
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
private int parent;
public int getParent() {
return parent;
}
public void setParent(int parent) {
this.parent = parent;
}
private int lchild;
public int getLchild() {
return lchild;
}
public void setLchild(int lchild) {
this.lchild = lchild;
}
private int rchild;
public int getRchild() {
return rchild;
}
public void setRchild(int rchild) {
this.rchild = rchild;
}
public HufmTree(){}
public HufmTree(int weight , int parent , int lchild , int rchild){
this.weight = weight;
this.parent = parent;
this.lchild = lchild;
this.rchild = rchild;
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int p1 , p2;
int small_1 , small_2;
int Max = 32767;
int nums = scanner.nextInt();
String[] c = new String[nums + 1];
int[] f = new int[nums + 1];
int M = 2 * nums - 1;
HufmTree[] hufmTree = new HufmTree[M + 1];
for (int i = 1; i <= nums; i++) {
c[i] = scanner.next();
f[i] = scanner.nextInt();
}
for (int i = 1; i <= M; i++) {
hufmTree[i] = new HufmTree(-1 , -1 , -1 , -1);
}
int tmp = 1;
for (int i = 1; i <= nums; i++) {
int weight = f[tmp++];
hufmTree[i].setWeight(weight);
}
for (int i = nums + 1; i <= M ; i++) {
p1 = p2 = 1;
small_1 = small_2 = Max;
for(int j = 1 ; j <= i - 1 ; j++){
if(hufmTree[j].getParent() == -1){
if(hufmTree[j].getWeight() < small_1){
small_2 = small_1;
p2 = p1;
small_1 = hufmTree[j].getWeight();
p1 = j;
} else if (hufmTree[j].getWeight() < small_2) {
small_2 = hufmTree[j].getWeight();