以现在的生产力,是做不到一天一篇博客了。这题给我难得不行了,花了两天时间在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的更多相关文章

  1. 05-树9 Huffman Codes

    哈夫曼树 Yes 需满足两个条件:1.HuffmanTree 结构不同,但WPL一定.子串WPL需一致 2.判断是否为前缀码 开始判断用的strstr函数,但其传值应为char *,不能用在strin ...

  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. java 多线程8(守护线程)

    比如:后台偷偷运行的那些,qq下载更新包 如果一个进程中只剩下了守护线程,那么守护线程也会死亡.. 一个线程默认都不是守护线程. 判断是否是守护线程:例:d.isDaemon(); 当一个线程随着你的 ...

  2. SharpZipLib要支持unicode的文件名称

    可以参考以下例子 ZipEntry entry = new ZipEntry(file); entry.IsUnicodeText = true; 这个方法找了很久,之前用其他帖子说的ZipConst ...

  3. css读书笔记4:字体和文本

    字体属性网页中的字体有3个来源:1.用户机器中安装的字体:2.保存在第三方网站上的字体.可以使用link标签把它们链接到页面中:3.保存在子集的web服务器上的字体.可以使用@font-face规则随 ...

  4. 32-HTML辅助方法

    顾名思义,HTML辅助方法(HTML Helper)就是用来辅助产生HTML之用,在开发View的时候一定会面对许多HTML标签,处理这些HTML的工作非常繁琐,为了降低View的复杂度,可以使用HT ...

  5. MVC 与传统的 webform 的比较

    代码架构方式 ASP 脚本语言和代码同置,每个请求页面对应一个物理文件 WebForm 代码后置 ,每个请求页面对应dll和一个.asp物理文件 MVC 代码分离,每个请求对应一个Action和一个V ...

  6. Objective-C:Foundation框架-结构体

    在Foundation中定义了很多常用结构体类型来简化我们的日常开发,这些结构体完全采用Objective-C定义,和我们自己定义的结构体没有任何区别,之所以由框架为我们提供完全是为了简化我们的开发. ...

  7. github注册使用以及体会

    一.个人介绍 我叫冯越,学号是1413042065,班级是网络工程143,兴趣爱好有打羽毛球,素描,读书,个人编程能力一般,行数没有计算过,大一学习C++时,实验题目一些书后的题目,大二学习数据结构时 ...

  8. easyui datagrid的列编辑

    [第十五篇]easyui datagrid的列编辑,同时插入两张表的数据进去   看图说话. 需求:插入两张表,上面的表单是第一张表的内容,下面的两个表格是第二张详情表的内容,跟第一张表的id关联 第 ...

  9. 转:PHP Composer 管理工具的介绍 这个相对清晰点

    转自:http://www.aichengxu.com/view/14872 一.PHP的一些臭历史 Dependency Manager For PHP,Composer.在Composer还没诞生 ...

  10. 如何对HTML进行编码和解码?

    答案:这个比较简单//HTML进行编码和解码Console.WriteLine(System.Web.HttpUtility.HtmlEncode("<h1>我是中文字符!< ...