Problem Description
Javac++ 一天在看计算机的书籍的时候,看到了一个有趣的东西!每一串字符都可以被编码成一些数字来储存信息,但是不同的编码方式得到的储存空间是不一样的!并且当储存空间大于一定的值的时候是不安全的!所以Javac++ 就想是否有一种方式是可以得到字符编码最小的空间值!显然这是可以的,因为书上有这一块内容--哈夫曼编码(Huffman Coding);一个字母的权值等于该字母在字符串中出现的频率。所以Javac++ 想让你帮忙,给你安全数值和一串字符串,并让你判断这个字符串是否是安全的?
Input
输入有多组case,首先是一个数字n表示有n组数据,然后每一组数据是有一个数值m(integer),和一串字符串没有空格只有包含小写字母组成!
Output
如果字符串的编码值小于等于给定的值则输出yes,否则输出no。
Sample Input
2 12 helloworld 66 ithinkyoucandoit
Sample Output
no yes//代码已被通过;//一开始没通过,看了一下网上的,发现只有一个字母的时候哈夫曼树是建不起来的,加了一段就通过了;#include<stdio.h> #include<malloc.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; struct minheap{ int *data; int size; int capacity; }; struct htnode{ int weight; struct htnode* left; struct htnode* right; }; minheap* creatheap(int maxsize); bool cmp(int a,int b); bool isfull(minheap* h); bool insert(minheap* h,int x); bool isempty(minheap* h); int deletemin(minheap* h); minheap* a=(minheap*)malloc(sizeof(minheap)); minheap* b=(minheap*)malloc(sizeof(minheap)); int main() { int n,M,sum=0; char str[10000]; int flag[10000]; int newnumber[10000]; int newcount=0; while(scanf("%d",&n)!=EOF) { while(n--) { memset(flag,0,sizeof(flag)); memset(newnumber,0,sizeof(newnumber)); newcount=0; int m; scanf("%d",&m); scanf("%s",str); for(int i=0;str[i]!='\0';i++) { sum=0; if(flag[i]!=0) continue; for(int j=0;str[j]!='\0';j++) if(str[j]==str[i]) { sum++; //统计每个字母出现次数; flag[j]=1; } newnumber[newcount]=sum; newcount++; } if(newcount==1) { if(newnumber[0]<=m) { printf("yes\n"); continue; } else { printf("no\n"); continue; } } sort(newnumber,newnumber+newcount,cmp); a=creatheap(10000); b=creatheap(10000); for(int i=0;newnumber[i]!=0;i++) insert(a,newnumber[i]); //权值的最小堆; for(int i=1;i<newcount;i++) { htnode* t=(htnode*)malloc(sizeof(htnode)); t->left=(htnode*)malloc(sizeof(htnode)); t->right=(htnode*)malloc(sizeof(htnode)); t->left->weight=deletemin(a); t->right->weight=deletemin(a); t->weight=t->left->weight+t->right->weight; insert(a,t->weight); } int sum2=0; for(int i=1;i<newcount;i++) sum2+=a->data[i]; if(sum2<=m) printf("yes\n"); else printf("no\n"); } } }minheap* creatheap(int maxsize) //创建最小堆; { minheap* h=(minheap*)malloc(sizeof(minheap)); h->data=(int*)malloc((maxsize+1)*sizeof(int)); h->size=0; h->capacity=maxsize; h->data[0]=0; return h; } bool isfull(minheap* h) { return(h->size==h->capacity); } bool insert(minheap* h,int x) //最小堆的插入; { int i; if(isfull(h)) { printf("man\n"); return false; } i=++h->size; for(;h->data[i/2]>x;i/=2) h->data[i]=h->data[i/2]; h->data[i]=x; return true; } bool isempty(minheap* h) { return (h->size==0); } int deletemin(minheap* h) //最小堆的删除; { int parent,child; int minitem,x; if(isempty(h)) { printf("kong\n"); return -1; } x=h->data[h->size--]; minitem=h->data[1]; for(parent=1;parent*2<=h->size;parent=child) { child=parent*2; if((child!=h->size)&&(h->data[child]>h->data[child+1])) child++; if(x<=h->data[child]) break; else h->data[parent]=h->data[child]; } h->data[parent]=x; return minitem; } bool cmp(int a,int b) { return a<=b; }