3345資料結構實驗之二叉樹六:哈夫曼編碼
Problem Description
字元的編碼方式有多種,除了大家熟悉的ASCII編碼,哈夫曼編碼(Huffman Coding)也是一種編碼方式,它是可變字長編碼。該方法完全依據字元出現概率來構造出平均長度最短的編碼,稱之為最優編碼。哈夫曼編碼常被用於資料檔案壓縮中,其壓縮率通常在20%~90%之間。你的任務是對從鍵盤輸入的一個字串求出它的ASCII編碼長度和哈夫曼編碼長度的比值。
Input
輸入資料有多組,每組資料一行,表示要編碼的字串。
Output
對應字元的ASCII編碼長度la,huffman編碼長度lh和la/lh的值(保留一位小數),資料之間以空格間隔。
Example Input
AAAAABCD
THE_CAT_IN_THE_HAT
Example Output
64 13 4.9
144 51 2.8
程式碼
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
char tree[500];//字串
int num[500];//統計字元個數
/*bool cmp(int x,int y)//將字串個數排序
{
return x<y;
}*/
int main()
{
while(~scanf("%s",&tree))
{
int len;
len=strlen(tree);
int sum=0;//哈夫曼碼數
int summ=0;//ACLL編碼數量
summ=len*8;
int queue[100];//用一個佇列來儲存陣列中相加的數
int k=0;
int cont=0;
memset(num,0,sizeof(num));//將計數的陣列全部初始化為0
for(int i=0;i<len;i++)//統計字元個數
{
num[tree[i]]++;
}
for(int i=0;i<500;i++)//將字元個數儲存起來
{
if(num[i]!=0)
{
queue[k++]=num[i];
}
}
while(k-cont>=2)
{
sort(queue+cont,queue+k);//排序的範圍
int x1=queue[cont];
cont++;
int x2=queue[cont];
cont++;
queue[k++]=x1+x2;
sum+=x1+x2;
}
printf("%d %d %.1lf\n",summ,sum,double((summ+0.0)/(sum+0.0)));//輸出強制型別轉換
}
return 0;
}
相關推薦
3345資料結構實驗之二叉樹六:哈夫曼編碼
Problem Description 字元的編碼方式有多種,除了大家熟悉的ASCII編碼,哈夫曼編碼(Huffman Coding)也是一種編碼方式,它是可變字長編碼。該方法完全依據字
資料結構實驗之二叉樹六:哈夫曼編碼(SDUT 3345)
題解:離散中的“最小生成樹(最優樹)”。 #include <bits/stdc++.h> using namespace std; void qusort(int l, int r, int a[]) { int x = a[l]; int i = l, j =
SDUTOJ3345資料結構實驗之二叉樹六:哈夫曼編碼
資料結構實驗之二叉樹六:哈夫曼編碼 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description 字元的編碼方式有多種,除了大家熟悉的ASC
SDUT3345資料結構實驗之二叉樹六:哈夫曼編碼
#include<bits/stdc++.h> using namespace std; int main() { char a[50]; while(~scanf("%s",a)) { int asc2=8*strlen
資料結構實驗之二叉樹六:哈夫曼編碼(最優二叉樹)
Problem Description 字元的編碼方式有多種,除了大家熟悉的ASCII編碼,哈夫曼編碼(Huffman Coding)也是一種編碼方式,它是可變字長編碼。該方法完全依據字元出現概率來構造出平均長度最短的編碼,稱之為最優編碼。哈夫曼編碼常被用於資
SDUT 3345 數據結構實驗之二叉樹六:哈夫曼編碼
g++ mit mil ade 入隊 一位 hat 一個 隊列 數據結構實驗之二叉樹六:哈夫曼編碼 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 字符的編碼方式有多種,除了大家
資料結構實驗之二叉樹一:樹的同構 (SDUT 3340)
題解:把原本結構體的左右子樹的型別定義成 int 型,用來存放這個結點的左右子樹的編號,分別建造兩棵二叉樹,按個比較,如果在第二棵樹中沒有找到,那麼就不用在判斷了。 #include <bits/stdc++.h> using namespace std; struct node
資料結構實驗之二叉樹八:(中序後序)求二叉樹的深度(SDUT 2804)
#include <stdio.h> #include <stdlib.h> #include <string.h> struct node { char data ; struct node *l,*r; }; struct node *cr
資料結構實驗之二叉樹七:葉子問題(SDUT 3346)
#include <bits/stdc++.h> using namespace std; struct node { char data; struct node *lc, *rc; }; char a[100]; int num = 0; struct node
資料結構實驗之二叉樹四:(先序中序)還原二叉樹 (SDUT 3343)
#include <bits/stdc++.h> using namespace std; struct node { char data; struct node *lc, *rc; }; char a[100],b[100]; int n; struct node
資料結構實驗之二叉樹五:層序遍歷 (SDUT 3344)
#include <bits/stdc++.h> using namespace std; struct node { char data; struct node *lc, *rc; }; char s[505]; int num; struct node *cre
資料結構實驗之二叉樹三:統計葉子數 SDUT 3342
#include <stdio.h> #include <string.h> struct node { char data; struct node *l,*r; }; struct node *root; char st[51]; int i; in
【二叉樹】SDUT 3342 資料結構實驗之二叉樹三:統計葉子數
Problem Description 已知二叉樹的一個按先序遍歷輸入的字元序列,如abc,,de,g,,f,,, (其中,表示空結點)。請建立二叉樹並求二叉樹的葉子結點個數。 Input 連續輸入多組資料,每組資料輸入一個長度小於50個字元的字串。 Output 輸出
SDUTOJ3344資料結構實驗之二叉樹五:層序遍歷
資料結構實驗之二叉樹五:層序遍歷 https://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Contest/contestproblem/cid/2711/pid/3344 Time Limit: 1000 ms Memo
3340--資料結構實驗之二叉樹一:樹的同構
現給定兩棵樹,請你判斷它們是否是同構的。 Input 輸入資料包含多組,每組資料給出2棵二叉樹的資訊。對於每棵樹,首先在一行中給出一個非負整數N (≤10),即該樹的結點數(此時假設結點從0到N−1編號);隨後N行,第i行對應編號第i個結點,給出該結點中儲存的1
資料結構實驗之二叉樹三:統計葉子數
Problem Description 已知二叉樹的一個按先序遍歷輸入的字元序列,如abc,de,g,f, (其中,表示空結點)。請建立二叉樹並求二叉樹的葉子結點個數。 Input 連續輸入多組資料,每
資料結構實驗之二叉樹三:統計葉子數(有返回值版)
Problem Description 已知二叉樹的一個按先序遍歷輸入的字元序列,如abc,de,g,f, (其中,表示空結點)。請建立二叉樹並求二叉樹的葉子結點個數。 Input 連續輸入多組資料,每組資料輸入一個長度小於50個字元的字串。 Output 輸出
資料結構實驗之二叉樹四:(先序中序)還原二叉樹
Problem Description 給定一棵二叉樹的先序遍歷序列和中序遍歷序列,要求計算該二叉樹的高度。 Input 輸入資料有多組,每組資料第一行輸入1個正整數N(1 <= N <=
資料結構實驗之二叉樹七:葉子問題
Problem Description 已知一個按先序輸入的字元序列,如abd,eg,cf,(其中,表示空結點)。請建立該二叉樹並按從上到下從左到右的順序輸出該二叉樹的所有葉子結點。 Input 輸入資
資料結構實驗之二叉樹三:統計葉子數 SDUT 3342
#include <stdio.h> #include <string.h> struct node { char data; struct node *l,*