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.pswp.cn/news/379947.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/379947.shtml
英文地址,請注明出處:http://en.pswp.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…