hdu1053 Entropy hdu2527 Safe Or Unsafe

裸裸的哈弗曼编码,求出哈弗曼编码的路径长度,注意整个字符串为一种字符的情况

View Code
#include<iostream>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
struct node
{
int u,w;
node(int a=0,int b=0):u(a),w(b){}
bool friend operator <(const node a,const node b)//重载操作符
{
return a.w>b.w;
}
};
int f[150],n,num,d[150],root,ans;
char str[5000];
priority_queue<node> Q;
vector<int> g[500];//用向量保存每一个节点的儿子
void BTree()//建哈弗曼树
{
node p1,p2;
while(!Q.empty())
{
p1=Q.top();Q.pop();
p2=Q.top();Q.pop();
g[num].push_back(p1.u);
g[num].push_back(p2.u);
if(Q.size()==0)
break;
Q.push(node(num,p1.w+p2.w));
num++;
}
}
void DFS(int v,int deep)
{
if(g[v].size()==0)//叶子节点的深度即为编码长度
{
ans+=f[v]*deep;
return ;
}
for(int i=0;i<2;i++)
DFS(g[v][i],deep+1);
}
int main()
{
while(scanf("%s",str)==1)
{
if(strcmp(str,"END")==0)
break;
while(!Q.empty())
Q.pop();
memset(d,0,sizeof(d));
for(int i=0;i<strlen(str);i++)
d[(int)str[i]]++;
n=0;
for(int i=0;i<128;i++)
if(d[i]!=0)
{
f[n]=d[i];
Q.push(node(n++,d[i]));
}
if(Q.size()==1)
{
printf("%d %d 8.0\n",strlen(str)*8,f[0]);
continue;
}
num=n;ans=0;
BTree();
DFS(num,0);
int tt=strlen(str)*8;
printf("%d %d %.1f\n",tt,ans,tt*1.0/ans);
for(int i=0;i<=num;i++)
g[i].clear();
}
return 0;
}

 hdu2527

Safe Or Unsafe

基本一样的题

View Code
#include<iostream>
#include<queue>
#include<algorithm>
#include<vector>
using namespace std;
struct node
{
int u,w;
node(int a=0,int b=0):u(a),w(b){}
bool friend operator <(const node a,const node b)//重载操作符
{
return a.w>b.w;
}
};
int f[150],n,num,d[150],root,ans;
char str[5000];
priority_queue<node> Q;
vector<int> g[500];//用向量保存每一个节点的儿子
void BTree()//建哈弗曼树
{
node p1,p2;
while(!Q.empty())
{
p1=Q.top();Q.pop();
p2=Q.top();Q.pop();
g[num].push_back(p1.u);
g[num].push_back(p2.u);
if(Q.size()==0)
break;
Q.push(node(num,p1.w+p2.w));
num++;
}
}
void DFS(int v,int deep)
{
if(g[v].size()==0)//叶子节点的深度即为编码长度
{
ans+=f[v]*deep;
return ;
}
for(int i=0;i<2;i++)
DFS(g[v][i],deep+1);
}
int main()
{
int T,cmp;
scanf("%d",&T);
while(T--)
{
scanf("%d",&cmp);
scanf("%s",str);
while(!Q.empty())
Q.pop();
memset(d,0,sizeof(d));
for(int i=0;i<strlen(str);i++)
d[(int)str[i]]++;
n=0;
for(int i=0;i<128;i++)
if(d[i]!=0)
{
f[n]=d[i];
Q.push(node(n++,d[i]));
}
num=n;ans=0;
if(n==1)
{
if(strlen(str)<=cmp)
printf("yes\n");
else printf("no\n");
continue;
}
BTree();
DFS(num,0);
if(ans<=cmp)
printf("yes\n");
else printf("no\n");
for(int i=0;i<=num;i++)
g[i].clear();
}
return 0;
}

 

转载于:https://www.cnblogs.com/nanke/archive/2012/02/15/2353383.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/379947.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java ListResourceBundle getContents()方法与示例

ListResourceBundle类的getContents()方法 (ListResourceBundle Class getContents() method) getContents() method is available in java.util package. getContents()方法在java.util包中可用。 getContents() method is used to get the contents into the form of an Obje…

DOM元素的所有子元素 .elements

.elements属性用来获取某个DOM元素的所有子元素&#xff0c;是个很有用的属性&#xff0c;可用于用className来获取指定元素等用途。 转载于:https://www.cnblogs.com/cly84920/archive/2008/08/06/4427136.html

sys.stdin.read和raw_input函数

sys.stdin.read函数 例子&#xff1a; import sysreadsys.stdin.read() for i in range(len(read)):print i,read[i],-1运行&#xff0c;当执行到readsys.stdin.read()会阻塞&#xff0c;等待我们输入 我们输入&#xff1a; h e当输入&#xff0c;ctrlD结束输入&#xff0c…

Java ObjectOutputStream writeFields()方法与示例

ObjectOutputStream类writeFields()方法 (ObjectOutputStream Class writeFields() method) writeFields() method is available in java.io package. 在java.io包中提供了writeFields()方法 。 writeFields() method is used to write the buffered fields to the ObjectOutpu…

利用带关联子查询Update语句更新数据

Update是T-sql中再简单不过的语句了&#xff0c;update table set columnexpression [where condition]&#xff0c;我们都会用到。但update的用法不仅于此&#xff0c;真正在开发的时候&#xff0c;灵活恰当地使用update可以达到事半功倍的效果。 假定有表Table1&#xff08;…

SQL Server 2000数据库移植到SQL Server 2008R2数据库服务器中碰到的”3145错误”及解决办法...

辛苦忙碌了一个星期终于安装配置好了TFS服务器&#xff0c;给每个团队成员分配了账户和邮箱。不过&#xff0c;老机器中的部分数据需要备份到新机器中&#xff0c;其中在移植一个使用DVBBS架设的论坛的时候&#xff0c;出了点问题&#xff0c;记录如下&#xff0c;以备查找&…

web安全----XSS漏洞之基本原理

0x01 概述 XSS为跨站脚本攻击&#xff0c;XSS攻击针对的是用户层面的攻击&#xff01;类型有反射型XSS、存储型XSS、DOM型XSS&#xff0c;这里的DOM可以理解为页面&#xff0c;或者是所有的标签、内容之和 0x02 反射型XSS 反射型XSS攻击流程为&#xff1a; 即&#xff1a; …

面向对象(匿名内部类与有名字内部类的比较)

A:匿名内部类 就是内部类的简化写法B:前提 这里的类可以是具体类也可以是抽象类C&#xff1a;格式 new 类名或者接口(){ //表示继承这个类或实现这个接口重写方法}D&#xff1a;本质是什么呢&#xff1f; 是一个继承了该类或者实现了该接口的子类匿名对象E&#xff1a;案…

如何在Python中针对一个值检查多个变量?

Given multiple variables and they are assigned some values, we have to test a value with these variables. 给定多个变量并为其分配了一些值&#xff0c;我们必须使用这些变量测试一个值。 Let, there are three variables a, b and c and we have to check whether one…

品析《桃花庵歌》

桃花庵歌 【明】唐寅&#xff08;YIN) 桃花坞里桃花庵&#xff0c;桃花庵下桃花仙&#xff1b; 桃花仙人种桃树&#xff0c;又摘桃花卖酒钱。 酒醒只在花前坐&#xff0c;酒醉还来花下眠&#xff1b; 半醒半醉日复日&#xff0c;花落花开年复年。 但愿老死花酒间&#xf…

面向对象(匿名内部类重写多个方法调用)

①匿名内部类只针对重写一个方法的时候使用; ②若有多个方法&#xff0c;通过匿名内部类进行调用的时候&#xff0c;需要一个一个进行调用,比较麻烦&#xff0c;所以不建议使用。 ③匿名内部类是无法向下转型的&#xff0c;向下转型需要子类的类名&#xff0c;这里面没有子类…

c++ 取两个链表的交集_使用C ++程序查找两个链表的交集

c 取两个链表的交集Problem statement: Write a C program to find the intersection of two single linked lists. 问题陈述&#xff1a;编写一个C 程序来查找两个单个链表的交集。 Example: 例&#xff1a; Let the first linked list be:6->5->2->9->NULLLet …

只在IE中显示

只在IE中显示div{display:none;display:block;_display:block}转载于:https://www.cnblogs.com/lishenglyx/archive/2008/08/21/1273089.html

web安全---XSS漏洞之标签使用2

所有标签已经测试完并可以使用&#xff0c;测试环境&#xff1a;DVWA的反射型XSS&#xff0c;等级low 0x01 <script>标签 <script>alert(2)</script> <script>alert(2)</script//0x02 <img>标签 <img src"x" onerroralert(1)…

Java——多线程(铁路售票系统案例)

问题&#xff1a;铁路售票&#xff0c;一共100张&#xff0c;通过四个窗口卖完。 要求&#xff1a;分别用 继承Thread类 和 实现Runnable接口 去实现 ①用继承Thread类去实现 package com.yy.syn;public class Demo3_Ticket { /*** 铁路售票&#xff0c;一共100张&#xff…

Java LocalDateTime类| 带示例的getDayOfWeek()方法

LocalDateTime类getDayOfWeek()方法 (LocalDateTime Class getDayOfWeek() method) getDayOfWeek() method is available in java.time package. getDayOfWeek()方法在java.time包中可用。 getDayOfWeek() method is used to get the field value day-of-week that is an enum …

软件测试方法大汇总

软件测试方法大汇总 软件测试方法种类繁多&#xff0c;记忆起来混乱&#xff0c; 如果把软件测试方法进行分类, 就会清晰很多。 我参考一些书籍和网上的资料&#xff0c; 把常用的软件测试方法列出来&#xff0c; 让大家对软件测试行业有个总体的看法。 从测试设计方法分类 测试…

web安全----xss工具使用3

XSSer 0x01 安装 环境&#xff1a;kali、python3&#xff08;必须是python3&#xff0c;kali默认为python2&#xff09; 安装步骤&#xff1a; git clone https://github.com/epsylon/xsser.git cd xsser sudo python3 setup.py install 使用命令&#xff1a; xsser -h查看…

Java——多线程(死锁)

死锁是指&#xff1a;两个或两个以上的进程在执行过程中&#xff0c;由于竞争资源或者由于彼此通信而造成的一种阻塞的现象&#xff0c;若无外力作用&#xff0c;它们都将无法推进下去。此时称系统处于死锁状态或系统产生了死锁&#xff0c;这些永远在互相等待的进程称为死锁进…

c# 前导0_C#| 用前导零填充整数

c# 前导0To pad an integer number with leading zero, we can use String.Format() method which is library method of String class in C#. 要用前导零填充整数&#xff0c;我们可以使用String.Format()方法&#xff0c;该方法是C&#xff03;中String类的库方法。 using S…