Safe Or Unsafe--hdu2527(哈夫曼树求WPL)
2015-12-29 13:20
344 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2527
用优先队列模拟
View Code
用优先队列模拟
#include<iostream> #include<stdio.h> #include<cstring> #include<algorithm> #include<queue> using namespace std; #define N 1100 #define met(a, b) memset(a, b, sizeof(a)) struct node { int x; friend bool operator<(node a, node b)///优先队列,实质是堆; { return a.x > b.x; } }; int main() { int T, n; int cnt ; char s ; scanf("%d", &T); while(T--) { met(s, 0); met(cnt, 0); scanf("%d", &n); scanf("%s", s); int len = strlen(s); for(int i=0; i<len; i++) cnt[s[i]-'a']++; priority_queue<node> Q; node p, q; for(int i=0; i<26; i++) { p.x = cnt[i]; if(cnt[i]) Q.push(p); } int ans = 0 ; if(Q.size()==1)///如果只有一个元素WPL是本身; ans = Q.top().x; while(Q.size()!=1) { node min1 = Q.top(); Q.pop();///找到两个权值最小的,构成新的节点 node min2 = Q.top(); Q.pop(); q.x = min1.x + min2.x; Q.push(q); ans += q.x; } /// printf("%d\n", ans); printf(ans <= n ? "yes\n":"no\n"); } return 0; }
View Code
相关文章推荐
- Bootstrap不同级别标题
- Jquery-获取兄弟元素
- JS跨域调用之JSONP--动态Script标签方式实现跨域
- html网页写一个时钟
- (转载)css高级选择器
- JS 正则表达式应用
- JavaScript学习 二、基础
- javascript跨域获取json数据
- js与jQuery操作select大全
- asp.net 框架搭建 mvc+nodejs
- jquery提交ajax Form(未完成)
- css3 新增属性集合
- QPushButton的样式表的顺序问题
- js解决中文乱码
- jQuery中attr()与prop()函数用法实例详解(附用法区别)
- html使用ul li 标签实现多级横向菜单
- 40 个重要的 HTML5 面试问题及答案
- js类、原型——学习笔记
- Html用ul li实现多级横向菜单
- (原创)node.js入门之一:express简单服务器搭建-Mac环境开发