In 1953, David A. Huffman published his paper "A Method for the Construction of Minimum-Redundancy Codes", and hence printed his name in the history of computer science. As a professor who gives the final exam problem on Huffman codes, I am encountering a big problem: the Huffman codes are NOT unique. For example, given a string "aaaxuaxz", we can observe that the frequencies of the characters 'a', 'x', 'u' and 'z' are 4, 2, 1 and 1, respectively. We may either encode the symbols as {'a'=0, 'x'=10, 'u'=110, 'z'=111}, or in another way as {'a'=1, 'x'=01, 'u'=001, 'z'=000}, both compress the string into 14 bits. Another set of code can be given as {'a'=0, 'x'=11, 'u'=100, 'z'=101}, but {'a'=0, 'x'=01, 'u'=011, 'z'=001} is NOT correct since "aaaxuaxz" and "aazuaxax" can both be decoded from the code 00001011001001. The students are submitting all kinds of codes, and I need a computer program to help me determine which ones are correct and which ones are not.
Input Specification:
Each input file contains one test case. For each case, the first line gives an integer N (2≤N≤63), then followed by a line that contains all the N distinct characters and their frequencies in the following format:
c[1] f[1] c[2] f[2] ... c[N] f[N]
where c[i]
is a character chosen from {'0' - '9', 'a' - 'z', 'A' - 'Z', '_'}, and f[i]
is the frequency of c[i]
and is an integer no more than 1000. The next line gives a positive integer M (≤1000), then followed by M student submissions. Each student submission consists of N lines, each in the format:
c[i] code[i]
where c[i]
is the i
-th character and code[i]
is an non-empty string of no more than 63 '0's and '1's.
Output Specification:
For each test case, print in each line either "Yes" if the student's submission is correct, or "No" if not.
Note: The optimal solution is not necessarily generated by Huffman algorithm. Any prefix code with code length being optimal is considered correct.
题意:
每个输入文件包含一个测试用例。对于每种情况,第一行给出一个整数N (2≤N≤63),然后后跟一行包含所有N不同的字符及其频率。下一行给出一个正整数M (≤1000),然后接着是M学生提交的材料。每个学生提交的作品包括N行
思路:
进行哈夫曼编码时用优先队列存储指向结构体的指针(或者可以用暴力每次找最小的两个)
然后先计算最短码长,与学生的进行比较,若相同则检查学生的是否是前缀码
#include <bits/stdc++.h>
using namespace std;
map<string,int>ma; //便于计算学生的最短码长
int ans = 0;
struct node { //进行哈夫曼编码的node
string s;
int fre;
node* l;
node* r;
int h;
};
struct CompareNodePointers {
bool operator()(const node* a, const node* b) const {
return a->fre > b->fre;
}
};
struct nodet { //进行测试学生的nodet
string s,ss; //s是字符,ss是学生的编码
};
node* in(node* j, node* k) {
node* t = new node;
t->l = j;
t->r = k;
t->fre = j->fre +k->fre ;
return t;
}
void check(node *t){
if(t->l && t->r ){
ans+=t->fre ;
check(t->l );
check(t->r );
}
}
int main() {
int n,m;
cin >> n;
node a[n + 1];
priority_queue<node*, vector<node*>, CompareNodePointers> q;
for (int i = 1; i <= n; i++) {
cin >> a[i].s >> a[i].fre;
a[i].l = a[i].r = NULL;
ma[a[i].s ] = a[i].fre ;
node* aa = &a[i];
q.push(aa);
}
node* t;
while (q.size() > 1) {
node* j = q.top();
q.pop();
node* k = q.top();
q.pop();
t = in(j, k);
if(!q.empty())q.push(t);
}
check(t); //计算最短码长
cin>>m;
while(m--){
int anss = 0;
vector<nodet>test;
for(int i = 1;i<=n;i++){
nodet j;
cin>>j.s >>j.ss ;
anss+=j.ss.length() * ma[j.s];
test.push_back(j);
}
int flag = 1;
if(anss != ans)flag = 0;
for (int i = 0; i < test.size(); ++i) {
for (int j = 0; j < test.size(); ++j) {
string o,p;
o = test[j].ss;
p = test[i].ss;
if (i != j && o.find(p) == 0) {
flag = 0; // 找到某个编码是另一个编码的前缀
break;
}
}
}
if(flag)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}