In 1953, David A. Huffman published his paper "A Method for the Construction of Minimum-Redundancy Codes", and henceprinted 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 canobserve that the frequencies of the characters 'a', 'x', 'u' and 'z' are 4, 2, 1 and 1, respectively. We may either encodethe symbols as {'a'=0, 'x'=10, 'u'=110, 'z'=111}, or in another way as {'a'=1, 'x'=01, 'u'=001, 'z'=000}, both compressthe 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. Thestudents are submitting all kinds of codes, and I need a computer program to help me determine which ones are correctand 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 a string of '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.
Sample Input: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 11Sample Output:
Yes Yes No No
______________________________________________________________________
就是Huffman code。
主要思路是比较WPL的大小,无论你怎么编码,huffman编码的cost是最低的;同时,需要比较二义性,即是否为前缀。这点在老师的云课堂上面有的。
这里需要注意的是,有专门的优先队列——就是堆,这是我没有见过的。
priority_queue<int, vector<int>, greater<int> > q;
在上面定义的是从小到大的一个queue,使用方法,参考这个文章。
http://blog.csdn.net/xkzju2010/article/details/46359827
另外,对于WPL的计算,这里只要把所有的加在一起就可以了,但是在循环结束之后,需要减去最后的那个。
主要是,在最后一次计算的时候,pop后给a,然后q为空,这里就会多加一次,需要再减去。
前缀的判断为,暴力,主要用到的是string的substr函数。
代码如下。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
struct num1
{
char c;
int f;
}in[65];
struct num2
{
char c;
string code;
}check[65];
int ind(char c,int len)
{
for(int i=0;i<len;i++)
{
if(in[i].c==c)
return i;
}
}
bool cmp(num2 a, num2 b)
{
return a.code.size()<b.code.size();//长度从小到大
}
bool qianzhui(int n)//前缀!!!需要判断
{
sort(check,check+n,cmp);
for(int i=0; i<n; i++)
{
string tmp=check[i].code;
for(int j=i+1; j<n; j++)
{
if(check[j].code.substr(0,tmp.size())==tmp)
return true;
}
}
return false;
}
int main(int argc, char** argv) {
int N=0,M=0;
int best=0;
// std::queue<int> q;
priority_queue<int, vector<int>, greater<int> > q;//!!!!!!!!!!!!!!本来是push进vector,然后排序,最后再push进queue,没想到的是有这种特殊的queue,果断改算法。
cin>>N;
for(int i=0; i<N; i++)
{
cin>>in[i].c>>in[i].f;
q.push(in[i].f);
}
// cout<<q.top();
int m=0;
while(!q.empty())
{
int a=0,b=0;
a=q.top();q.pop();
if(!q.empty())
{
b=q.top();q.pop();
q.push(a+b);
}
m=a+b;
best+=m;
}
best-=m;
// cout<<best;
cin>>M;
for(int i=0; i<M; i++)
{
int cost=0;
char code[32];
for(int j=0; j<N; j++)
{
cin>>check[j].c>>check[j].code;;
cost+=check[j].code.size()*in[ind(check[j].c,N)].f;
}
if(cost <= best)
{
if(qianzhui(N)) printf("No\n");
else printf("Yes\n");
}
else printf("No\n");
}
return 0;
}