PAT 05-树8 Huffman Codes
以现在的生产力,是做不到一天一篇博客了。这题给我难得不行了,花了两天时间在PAT上还有测试点1没过,先写上吧。记录几个做题中的难点:1、本来比较WPL那块我是想用一个函数实现的,无奈我对传字符串数组无可奈何;2、实在是水平还不够,做题基本上都是要各种参考,当然以课件(网易云课堂《数据结构》(陈越,何钦铭))中给的方法为主,可是呢,关于ElementType的类型我一直确定不下来,最后还是参考了园友糙哥(http://www.cnblogs.com/liangchao/p/4286598.html#3158189)的博客;3、关于如何判断是否为前缀码的方法,我是用的最暴力的一一对比,不知如何能更好的实现。好了,具体的题目及测试点1未过的代码实现如下
/*
Name:
Copyright:
Author:
Date: 07/04/15 11:05
Description:
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 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 11
Sample Output:
Yes
Yes
No
No
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h> #define MinData 0 typedef struct TreeNode
{
int Weight;
struct TreeNode * Left, * Right;
}HuffmanTree, * pHuffmanTree;
typedef struct HeapStruct
{
pHuffmanTree Elements;
int Size;
int Capacity;
}MinHeap, * pMinHeap; pMinHeap Create(int MaxSize);
void Insert(pMinHeap pH, HuffmanTree item);
pHuffmanTree Huffman(pMinHeap pH);
pHuffmanTree DeleteMin(pMinHeap pH);
int getWPL(pHuffmanTree pT, int layer, int WPL); int main()
{
// freopen("in.txt", "r", stdin); // for test
int N, i; // get input
scanf("%d", &N);
int a[N];
char ch;
for(i = ; i < N; i++)
{
getchar();
scanf("%c", &ch);
scanf("%d",&a[i]);
} int WPL = ; // build min-heap and Huffman tree
pMinHeap pH;
HuffmanTree T;
pH = Create(N);
for(i = ; i < N; i++)
{
T.Weight = a[i];
T.Left = NULL;
T.Right = NULL;
Insert(pH, T);
}
pHuffmanTree pT;
pT = Huffman(pH); WPL = getWPL(pT, , WPL); // compare WPL
int M, j, k;
scanf("%d", &M);
int w[M], flag[M];
char s[N][N + ];
for(i = ; i < M; i++)
{
w[i] = ;
flag[i] = ;
for(j = ; j < N; j++)
{
getchar();
scanf("%c", &ch);
scanf("%s", s[j]);
w[i] += strlen(s[j]) * a[j];
}
if(w[i] == WPL)
{
flag[i] = ;
for(j = ; j < N; j++)
{
for(k = j + ; k < N; k++)
{
if(strlen(s[j]) != strlen(s[k]))
{
if(strlen(s[j]) >strlen(s[k]))
if(strstr(s[j], s[k]) == s[j])
{
flag[i] = ;
break;
}
else
if(strstr(s[k], s[j]) == s[k])
{
flag[i] = ;
break;
}
}
}
}
}
} for(i = ; i < M; i++)
{
if(flag[i])
printf("Yes\n");
else
printf("No\n");
}
// fclose(stdin); // for test
return ;
} pMinHeap Create(int MaxSize)
{
pMinHeap pH = (pMinHeap)malloc(sizeof(MinHeap));
pH->Elements = (pHuffmanTree)malloc((MaxSize + ) * sizeof(HuffmanTree));
pH->Size = ;
pH->Capacity = MaxSize;
pH->Elements[].Weight = MinData; return pH;
} void Insert(pMinHeap pH, HuffmanTree item)
{
int i; i = ++pH->Size;
for(; pH->Elements[i / ].Weight > item.Weight; i /= )
pH->Elements[i] = pH->Elements[i / ];
pH->Elements[i] = item;
} pHuffmanTree Huffman(pMinHeap pH)
{
int i;
pHuffmanTree pT; for(i = ; i < pH->Capacity; i++)
{
pT = (pHuffmanTree)malloc(sizeof(HuffmanTree));
pT->Left = DeleteMin(pH);
pT->Right = DeleteMin(pH);
pT->Weight = pT->Left->Weight + pT->Right->Weight;
Insert(pH, *pT);
}
pT = DeleteMin(pH); return pT;
} pHuffmanTree DeleteMin(pMinHeap pH)
{
int Parent, Child;
pHuffmanTree pMinItem;
HuffmanTree temp; pMinItem = (pHuffmanTree)malloc(sizeof(HuffmanTree));
*pMinItem = pH->Elements[];
temp = pH->Elements[pH->Size--];
for(Parent = ; Parent * <= pH->Size; Parent = Child)
{
Child = Parent * ;
if((Child != pH->Size) && (pH->Elements[Child].Weight > pH->Elements[Child + ].Weight))
Child++;
if(temp.Weight <= pH->Elements[Child].Weight)
break;
else
pH->Elements[Parent] = pH->Elements[Child];
}
pH->Elements[Parent] = temp; return pMinItem;
} int getWPL(pHuffmanTree pT, int layer, int WPL)
{
if(pT->Left == NULL && pT->Right == NULL)
WPL += layer * pT->Weight;
else
{
WPL = getWPL(pT->Left, layer + , WPL);
WPL = getWPL(pT->Right, layer + , WPL);
} return WPL;
}
PAT 05-树8 Huffman Codes的更多相关文章
- 05-树9 Huffman Codes
哈夫曼树 Yes 需满足两个条件:1.HuffmanTree 结构不同,但WPL一定.子串WPL需一致 2.判断是否为前缀码 开始判断用的strstr函数,但其传值应为char *,不能用在strin ...
- 05-树9 Huffman Codes及基本操作
哈夫曼树与哈弗曼编码 哈夫曼树 带权路径长度(WPL):设二叉树有n个叶子结点,每个叶子结点带有权值 Wk,从根结点到每个叶子结点的长度为 Lk,则每个叶子结点的带权路径长度之和就是: WPL = 最 ...
- pta5-9 Huffman Codes (30分)
5-9 Huffman Codes (30分) In 1953, David A. Huffman published his paper "A Method for the Const ...
- 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 ...
- 数据结构慕课PTA 05-树9 Huffman Codes
题目内容 In 1953, David A. Huffman published his paper "A Method for the Construction of Minimum-Re ...
- 哈夫曼树(Huffman Tree)与哈夫曼编码
哈夫曼树(Huffman Tree)与哈夫曼编码(Huffman coding)
- 05-树9 Huffman Codes (30 分)
In 1953, David A. Huffman published his paper "A Method for the Construction of Minimum-Redunda ...
- 05-树9 Huffman Codes (30 分)
In 1953, David A. Huffman published his paper "A Method for the Construction of Minimum-Redunda ...
- Huffman codes
05-树9 Huffman Codes(30 分) In 1953, David A. Huffman published his paper "A Method for the Const ...
随机推荐
- jmeter 建立一个JMS点对点测试计划
确保所需的jar文件在JMeter的 自由 目录中. 如果他们不是,关闭JMeter, 重启JMeter复制jar文件. 看到 开始 获取详细信息. 测试的设置是1与5线程发送4 thread ...
- text-overflow:ellipsis实现超出隐藏时省略号显示
text-overflow:ellipsis;要达到的效果是:文字超出容器宽度时,文字被隐藏的文字用省略号代替.所以该属性只能用于块状元素或行内块元素中,对行内元素是不起作用的. 一般和white-s ...
- javaSE基础之基本细节注解
1. 对于多行注释而言,不能进行嵌套注释.....! /* dada /* d adasdas */ */ 只是不被允许的.... 2.对于记事本编程......如果竹类是公有类,则必须保证类名和为 ...
- iOS 10 消息推送(UserNotifications)秘籍总结(一)
前言 之前说会单独整理消息通知的内容,但是因为工(就)作(是)的(很)事(懒)没有更新文章,违背了自己的学习的初衷.因为互联网一定要有危机意识,说不定眼一睁,我们就out丢了饭碗. 图片来源网络.jp ...
- 如何查看,关闭和开启selinux
以下介绍一下SELinux相关的工具/usr/bin/setenforce 修改SELinux的实时运行模式setenforce 1 设置SELinux 成为enforcing模式setenforce ...
- DataProcessing
clear load X4058 [m,n]=size(X528); Mean=zeros(1,n); Dev=zeros(1,n); for i=1:n Xi=X528(1:end-1,i); Xi ...
- android关于uses-permission权限列表
在编写Android程序时经常会忘记添加权限,下面是网上收集的关于Androiduses-permission的资料,方便查找~ android.permission.ACCESS_CHECKIN_P ...
- ASP.NET-【Excel】-将Excel中的数据批量加载到SQLserver数据库
用到了一个SqlBulkCopy的类 核心代码分析 代码我还没有测试过 string excelConnectionString = string.Format("Provider=Micr ...
- double int char 数据类型
贴心的limits... 测试代码: #include <iostream> #include <stdio.h> #include <limits> #inclu ...
- shell学习记录003-cat命令
cat 命令一般用于文件的查看 cat -s file #可以去除文件中多余的上下空行 cat -T file #Python编程中会用到的制表符会在该命令中体现出来 cat -n file ...