哈夫曼树

Yes 需满足两个条件:1.HuffmanTree 结构不同,但WPL一定。子串WPL需一致

          2.判断是否为前缀码

  开始判断用的strstr函数,但其传值应为char *,不能用在string类型。所以后来改用substr。

substr(start,length);start为子串起始位置,length为从起始位置的长度。

 #include <iostream>
#include <string>
using namespace std; int main()
{
string str = "",endStr;
endStr = str.substr(,);
cout<< endStr <<endl;
return ;
}
//output:123

View Eg Code

  因为这里用了容器优先队列,且判断前缀码用了暴力求解,substr(start,length);函数。所以 要点:最大N&M,code长度等于63  超时了。

  自己写,会快。

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 NNdistinct 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', '_'}, andf[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 MM student submissions. Each student submission consists of NN 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.

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 11

Sample Output:

Yes
Yes
No
No
 #include <iostream>
#include <cstdio>
#include <string>
#include <queue>
#include <algorithm>
using namespace std; struct HuffTreeNode
{
char c;
int f;
};
struct HuffTreeNode HuffNode[]; struct strNod
{
char c;
string code;
};
struct strNod strNode[]; bool compare(strNod a, strNod b)
{
return a.code.size() < b.code.size();/*长度从小到大排序 */
} /*strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串。
如果是,则该函数返回str2在str1中首次出现的地址;否则,返回NULL。*/
/*[Error] cannot convert 'std::string {aka std::basic_string<char>}' to 'const char*' for argument '1' to 'char* strstr(const char*, const char*)'*/
/*是子串返回true ,否则false */
bool isStrstr(int N)
{
sort(strNode, strNode+N, compare);
for(int i = ; i < N; i ++) {
for(int j = i + ; j < N; j++) {
if( strNode[j].code.substr( , strNode[i].code.size() ) == strNode[i].code )
return true;
}
}
return false;
} int main()
{
int N;
scanf("%d",&N);
priority_queue<int, vector<int>, greater<int> > pq;
for(int i = ; i < N; i++) {
getchar(); /*排除回车 空格的影响 */
scanf("%c",&HuffNode[i].c);
scanf("%d",&HuffNode[i].f);
pq.push(HuffNode[i].f);
}
/*计算HuffmanTree WPL*/
int WPL = ;
int smallA,smallB;
while( !pq.empty() ) {
smallA = pq.top(); pq.pop(); /*取出两个最小的 */
if( !pq.empty() ) {
smallB = pq.top(); pq.pop();
smallA += smallB;
pq.push(smallA); /*求和后push进优先队列 */
}
WPL += smallA;
}
WPL -= smallA; /*求出WPL */
/* printf("WPL = %d",WPL);*/
int M;
scanf("%d",&M);
for(int i = ; i < M; i++) {
int checkWpl = ;
for(int j = ; j < N; j++) {
cin >> strNode[j].c >> strNode[j].code;
checkWpl += strNode[j].code.size() * HuffNode[j].f; //计算wpl
}
/* printf("checkWpl = %d",checkWpl);*/
if(checkWpl == WPL) { /*WPL符合,判断是否是前缀 */
if( isStrstr(N) ) /*有子串,不符合 */
printf("No\n");
else
printf("Yes\n");
}
else
printf("No\n");
}
return ;
}

事实证明,自己写最小堆,并不能达到要求。大头应该是字符串比较那里。

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std; #define MINDATA 0 /* 该值应根据具体情况定义为小于堆中所有可能元素的值 */
#define ERROR -1 struct HNode {
int *Data; /* 存储元素的数组 */
int Size; /* 堆中当前元素个数 */
int Capacity; /* 堆的最大容量 */
};
typedef struct HNode *MinHeap; /* 堆的类型定义 */ struct TreeNode {
char c;
int f;
};
struct TreeNode HuffNode[]; struct strNod
{
char c;
string code;
};
struct strNod strNode[];
MinHeap CreateHeap( int MaxSize );
bool IsFull( MinHeap H );
bool Insert( MinHeap H, int X );
bool IsEmpty( MinHeap H );
int DeleteMin( MinHeap H ); bool compare(strNod a, strNod b)
{
return a.code.size() < b.code.size();/*长度从小到大排序 */
} /*strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串。
如果是,则该函数返回str2在str1中首次出现的地址;否则,返回NULL。*/
/*[Error] cannot convert 'std::string {aka std::basic_string<char>}' to 'const char*' for argument '1' to 'char* strstr(const char*, const char*)'*/
/*是子串返回true ,否则false */
bool isStrstr(int N)
{
sort(strNode, strNode+N, compare);
for(int i = ; i < N; i ++) {
for(int j = i + ; j < N; j++) {
if( strNode[j].code.substr( , strNode[i].code.size() ) == strNode[i].code )
return true;
}
}
return false;
} int main()
{
int N;
scanf("%d",&N);
MinHeap Heap = CreateHeap(N);
for(int i = ; i < N; i++) {
getchar(); /*排除回车 空格的影响 */
scanf("%c",&HuffNode[i].c);
scanf("%d",&HuffNode[i].f);
Insert(Heap, HuffNode[i].f);
} //计算WPL的值
int WPL = ;
int smallA, smallB;
while( !IsEmpty(Heap) ) {
smallA = DeleteMin(Heap);
if( !IsEmpty(Heap) ) {
smallB = DeleteMin(Heap);
smallA += smallB;
Insert( Heap, smallA);
}
WPL += smallA;
}
WPL -= smallA;
// printf("WPL = %d\n",WPL); int M;
scanf("%d",&M);
for(int i = ; i < M; i++) {
int checkWpl = ;
for(int j = ; j < N; j++) {
cin >> strNode[j].c >> strNode[j].code;
checkWpl += strNode[j].code.size() * HuffNode[j].f; //计算wpl
}
/* printf("checkWpl = %d",checkWpl);*/
if(checkWpl == WPL) { /*WPL符合,判断是否是前缀 */
if( isStrstr(N) ) /*有子串,不符合 */
printf("No\n");
else
printf("Yes\n");
}
else
printf("No\n");
}
return ;
} /* 创建容量为MaxSize的空的最小堆 */
MinHeap CreateHeap( int MaxSize )
{ MinHeap H = (MinHeap)malloc(sizeof(struct HNode));
H->Data = (int*)malloc((MaxSize+)*sizeof(int));
H->Size = ;
H->Capacity = MaxSize;
H->Data[] = MINDATA; /* 定义"哨兵"为小于堆中所有可能元素的值*/
return H;
} bool IsFull( MinHeap H )
{
return (H->Size == H->Capacity);
} /* 将元素X插入最小堆H,其中H->Data[0]已经定义为哨兵 */
bool Insert( MinHeap H, int X )
{
if ( IsFull(H) ) {
printf("最小堆已满");
return false;
}
int i = ++H->Size; /* i指向插入后堆中的最后一个元素的位置 */
for ( ; H->Data[i/] > X; i /= )
H->Data[i] = H->Data[i/]; /* 上滤X */
H->Data[i] = X; /* 将X插入 */
return true;
} bool IsEmpty( MinHeap H )
{
return (H->Size == );
} /* 从最小堆H中取出键值为最小的元素,并删除一个结点 */
int DeleteMin( MinHeap H )
{
int Parent, Child;
int MinItem, X; if ( IsEmpty(H) ) {
printf("最小堆已为空");
return ERROR;
} MinItem = H->Data[]; /* 取出根结点存放的最小值 */
/* 用最小堆中最后一个元素从根结点开始向上过滤下层结点 */
X = H->Data[H->Size--]; /* 注意当前堆的规模要减小 */
for( Parent = ; Parent * <= H->Size; Parent = Child ) {
Child = Parent * ;
if( (Child != H->Size) && (H->Data[Child] > H->Data[Child+]) )
Child++; /* Child指向左右子结点的较小者 */
if( X <= H->Data[Child] ) break; /* 找到了合适位置 */
else /* 下滤X */
H->Data[Parent] = H->Data[Child];
}
H->Data[Parent] = X; return MinItem;
}

剪枝、。。。失败

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std; #define MINDATA 0 /* 该值应根据具体情况定义为小于堆中所有可能元素的值 */
#define ERROR -1 struct HNode {
int *Data; /* 存储元素的数组 */
int Size; /* 堆中当前元素个数 */
int Capacity; /* 堆的最大容量 */
};
typedef struct HNode *MinHeap; /* 堆的类型定义 */ struct TreeNode {
char c;
int f;
};
struct TreeNode HuffNode[]; struct strNod
{
char c;
string code;
};
struct strNod strNode[];
MinHeap CreateHeap( int MaxSize );
bool IsFull( MinHeap H );
bool Insert( MinHeap H, int X );
bool IsEmpty( MinHeap H );
int DeleteMin( MinHeap H ); bool compare(strNod a, strNod b)
{
return a.code.size() < b.code.size();/*长度从小到大排序 */
} /*strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串。
如果是,则该函数返回str2在str1中首次出现的地址;否则,返回NULL。*/
/*[Error] cannot convert 'std::string {aka std::basic_string<char>}' to 'const char*' for argument '1' to 'char* strstr(const char*, const char*)'*/
/*是子串返回true ,否则false */
bool isStrstr(int N)
{
sort(strNode, strNode+N, compare);
for(int i = ; i < N; i ++) {
for(int j = i + ; j < N; j++) {
if( strNode[j].code.substr( , strNode[i].code.size() ) == strNode[i].code )
return true;
}
}
return false;
} int main()
{
int N;
scanf("%d",&N);
MinHeap Heap = CreateHeap(N);
for(int i = ; i < N; i++) {
getchar(); /*排除回车 空格的影响 */
scanf("%c",&HuffNode[i].c);
scanf("%d",&HuffNode[i].f);
Insert(Heap, HuffNode[i].f);
} //计算WPL的值
int WPL = ;
int smallA, smallB;
while( !IsEmpty(Heap) ) {
smallA = DeleteMin(Heap);
if( !IsEmpty(Heap) ) {
smallB = DeleteMin(Heap);
smallA += smallB;
Insert( Heap, smallA);
}
WPL += smallA;
}
WPL -= smallA;
// printf("WPL = %d\n",WPL); int M; scanf("%d",&M);
for(int i = ; i < M; i++) {
int checkWpl = ;
bool overFlag = false; //其中一个字符串超出长度
for(int j = ; j < N; j++) {
cin >> strNode[j].c >> strNode[j].code;
if(strNode[j].code.size() > N-) { //如果有size超出N-1 一定不满足WPL 这里卡一刀希望快些
overFlag = true;
break;
}
checkWpl += strNode[j].code.size() * HuffNode[j].f; //计算wpl
}
/* printf("checkWpl = %d",checkWpl);*/
if(checkWpl == WPL && !overFlag) { /*WPL符合,判断是否是前缀 */
if( isStrstr(N) ) /*有子串,不符合 */
printf("No\n");
else
printf("Yes\n");
}
else
printf("No\n");
}
return ;
} /* 创建容量为MaxSize的空的最小堆 */
MinHeap CreateHeap( int MaxSize )
{ MinHeap H = (MinHeap)malloc(sizeof(struct HNode));
H->Data = (int*)malloc((MaxSize+)*sizeof(int));
H->Size = ;
H->Capacity = MaxSize;
H->Data[] = MINDATA; /* 定义"哨兵"为小于堆中所有可能元素的值*/
return H;
} bool IsFull( MinHeap H )
{
return (H->Size == H->Capacity);
} /* 将元素X插入最小堆H,其中H->Data[0]已经定义为哨兵 */
bool Insert( MinHeap H, int X )
{
if ( IsFull(H) ) {
printf("最小堆已满");
return false;
}
int i = ++H->Size; /* i指向插入后堆中的最后一个元素的位置 */
for ( ; H->Data[i/] > X; i /= )
H->Data[i] = H->Data[i/]; /* 上滤X */
H->Data[i] = X; /* 将X插入 */
return true;
} bool IsEmpty( MinHeap H )
{
return (H->Size == );
} /* 从最小堆H中取出键值为最小的元素,并删除一个结点 */
int DeleteMin( MinHeap H )
{
int Parent, Child;
int MinItem, X; if ( IsEmpty(H) ) {
printf("最小堆已为空");
return ERROR;
} MinItem = H->Data[]; /* 取出根结点存放的最小值 */
/* 用最小堆中最后一个元素从根结点开始向上过滤下层结点 */
X = H->Data[H->Size--]; /* 注意当前堆的规模要减小 */
for( Parent = ; Parent * <= H->Size; Parent = Child ) {
Child = Parent * ;
if( (Child != H->Size) && (H->Data[Child] > H->Data[Child+]) )
Child++; /* Child指向左右子结点的较小者 */
if( X <= H->Data[Child] ) break; /* 找到了合适位置 */
else /* 下滤X */
H->Data[Parent] = H->Data[Child];
}
H->Data[Parent] = X; return MinItem;
}

05-树9 Huffman Codes的更多相关文章

  1. PAT 05-树8 Huffman Codes

    以现在的生产力,是做不到一天一篇博客了.这题给我难得不行了,花了两天时间在PAT上还有测试点1没过,先写上吧.记录几个做题中的难点:1.本来比较WPL那块我是想用一个函数实现的,无奈我对传字符串数组无 ...

  2. 05-树9 Huffman Codes及基本操作

    哈夫曼树与哈弗曼编码 哈夫曼树 带权路径长度(WPL):设二叉树有n个叶子结点,每个叶子结点带有权值 Wk,从根结点到每个叶子结点的长度为 Lk,则每个叶子结点的带权路径长度之和就是: WPL = 最 ...

  3. pta5-9 Huffman Codes (30分)

    5-9 Huffman Codes   (30分) In 1953, David A. Huffman published his paper "A Method for the Const ...

  4. PTA 05-树9 Huffman Codes (30分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/671 5-9 Huffman Codes   (30分) In 1953, David ...

  5. 数据结构慕课PTA 05-树9 Huffman Codes

    题目内容 In 1953, David A. Huffman published his paper "A Method for the Construction of Minimum-Re ...

  6. 哈夫曼树(Huffman Tree)与哈夫曼编码

    哈夫曼树(Huffman Tree)与哈夫曼编码(Huffman coding)

  7. 05-树9 Huffman Codes (30 分)

    In 1953, David A. Huffman published his paper "A Method for the Construction of Minimum-Redunda ...

  8. 05-树9 Huffman Codes (30 分)

    In 1953, David A. Huffman published his paper "A Method for the Construction of Minimum-Redunda ...

  9. Huffman codes

    05-树9 Huffman Codes(30 分) In 1953, David A. Huffman published his paper "A Method for the Const ...

随机推荐

  1. 用ie调试的时候显示:脚本调试程序无法连接到目标进程,已附加调试程序。

    解决方案如图所示: 解决方案: 在internet的选项工具中选中高级然后去掉禁止脚本调试的情况:

  2. Hive基础之HiveServer2 JDBC的使用

    启动HiveServer2: cd $HIVE_HOME/bin 以后台方式默认端口启动HiveServer2(默认端口是10000):hiveserver2 & 以后台方式指定端口的方式启动 ...

  3. lua绑定c++的时候常见得错误

    1 Error parsing reanslation unit 这种情况,首先来说你的python 2.7以及他的插件安装是完整的,最可能的原因就是自己写的ini文件,header路径错误,可以把这 ...

  4. selenium项目总结(UI自动化测试平台搭建、测试报告生成、邮件转发)

    最近博主完成了一个web端UI自动化平台测试环境搭建,包括常用的操作流程组合成方法,组织测试用例,利用框架及报告文件生成,最后通过邮件添加附件的格式发送. 首先UI自动化平台的核心是selenium+ ...

  5. 游戏设计模式系列(一)—— 单线逻辑&&数据驱动,搞定最容易卡死的结算界面

    从事游戏行业1年多了,个中心酸不知从何说起.抛开非技术的不说,一个开发者需要面对的最大问题,可能就是和策划频繁改变的需求做斗争了吧,这时候就体现了设计模式的重要性,抛开正式的设计方式不说,先讲讲我1年 ...

  6. 为什么你不应该自行更新 Drupal 网站?

    (译注:这篇文章主要还是针对于非专业人员及个人Drupal站长,对于专业的 Drupal 团队和公司而言 Drupal 的升级更新都有规范的操作流程,完全是家常便饭,不可能出现文中出现的这些情况.尽管 ...

  7. MSP430F149学习之路——按键

    代码一: /********************************** 程序功能:用按键控制LED灯熄灭 ***********************************/ #incl ...

  8. EXTJS 密码确认与验证

    extjs 框架是一个非常优秀的前端框架,提供了丰富的功能与炫丽的界面展示,在用 extjs 创建表单时,特别是在注册或修改密码的时候,要对密码进行确认,这里就密码确认一致性验证和大家分享自己的心得与 ...

  9. WebService之基于REST机制的实现实例(Java版)

    REST是REpresentational State Transfer的缩写(一般中文翻译为表述性状态转移).2000年Roy Fielding博士在他的博士论文“Architectural Sty ...

  10. 二模10day2解题报告

    T1.最多因子数(divisors) 给出范围l,r求其中约数和最大的最小整数. 非常深井冰的题目:如果特判加暴力的话分数低的可怜 AC做法要用到分解质因数和线性筛(这俩好写),然而,一个一个枚举还是 ...