找回密碼 或 安全提問
 註冊
|註冊|登錄

伊莉討論區

搜索
請尊重及感激所有版主付出和奉獻尊貴會員無限使用任何功能伊莉需要你的贊助和支持
無碼神奇寶貝出包王女vr中字rpg無碼 ge
cawd 754假太監:成人綜藝光之美少渡口ヤリステ無冬之夜

休閒聊天興趣交流學術文化旅遊交流飲食交流家庭事務PC GAMETV GAME
熱門線上其他線上感情感性寵物交流家族門派動漫交流貼圖分享BL/GL
音樂世界影視娛樂女性頻道潮流資訊BT下載區GB下載區下載分享短片
電腦資訊數碼產品手機交流交易廣場網站事務長篇小說體育運動時事經濟
上班一族博彩娛樂

台詞沒背好不要出來丟

囂張的中國人在日本被

平溪 15歲無照騎士 逆

黃狗死命咬小女孩

[繁]CODE GEASS 奪回

女子搭台鐵時亂呻吟
C & C++ 語言C# 語言Visual Basic 語言PHP 語言JAVA 語言
查看: 4282|回復: 2
打印上一主題下一主題

[問題]一題有關huffman coding的問題[複製鏈接]

帖子
488
積分
197 點
潛水值
7050 米
跳轉到指定樓層
樓主
發表於 2011-3-20 12:05 PM|只看該作者|倒序瀏覽
題目大概是這樣
INPUT: one single line containing only"A-Z""a-z""0-9",totally 62posiible characters
            of length less than 10000.
OUTPUT:the number of bits of its Huffman encoding
EX: input: AAABBBAABCAAAABD
      output: 25


不用給完整的code  因為現在是完全不知如何下手
有沒有什麼建議呢?
分享分享0收藏收藏0支持支持0
若新密碼無法使用,可能是數據未更新。請使用舊密碼看看。

使用道具檢舉

Rank: 2Rank: 2

帖子
85
積分
594 點
潛水值
22084 米
頭香
發表於 2011-3-22 12:03 AM|只看該作者
所有積分大於負-100的壞孩子,將可獲得重新機會成為懲罰生,權限跟幼兒生一樣。
提供一個使用HEAP的方法

1.統計各個字元的數量
2.全部PUSH到HEAP中
3.若HEAP大小等於1則跳到步驟6否則繼續
4.POP出2個值最小的元素並丟入一新元素其大小是兩元素的大小和
5.跳回步驟3
6.加總每個字元的取出次數*字元數即為答案

EX:AAABBBAABCAAAABD
A9個 B5個 C1個 D1個
取出C和D丟入(C+D)
取出(C+D)和B丟入(B+C+D)
取出(B+C+D)和A丟入(A+B+C+D)
...
瀏覽完整內容,請先 註冊登入會員
所有積分大於負-100的壞孩子,將可獲得重新機會成為懲罰生,權限跟幼兒生一樣。

使用道具檢舉

  中學生(1000/4000)

唷吼吼齁

Rank: 3Rank: 3Rank: 3

帖子
153
積分
1615 點
潛水值
13857 米
3
發表於 2011-4-5 11:56 PM|只看該作者
如果瀏覽伊莉時速度太慢或無法連接,可以使用其他分流瀏覽伊莉,www01.eyny.com(02,03)。
小弟提供一個方法
我是把每一個Huffman碼全部算出來
然後再去算結果
  1. #include <iostream>
  2. using namespace std;
  3. struct Huffman {
  4.         char ch ;
  5.         int times ;
  6.         char *code ;
  7.         bool isLeaf ;
  8.         Huffman *r , *l , *next ;
  9. } ;
  10. void InsertionSort ( Huffman *x , int size ) ;
  11. Huffman * MergerCode ( Huffman *x , int size ) {
  12.         if ( size == 1 )                                        //如果size = 1的時候,代表整棵樹已經建立完成
  13.                 return x ;                                                //回傳x
  14.         InsertionSort(x , size ) ;                        //把x根據times & ch 排序
  15.        
  16.        

  17.         Huffman tmp ;
  18.         tmp.ch = NULL ;
  19.         tmp.times = x[0].times + x[1].times ;        //把最小的前兩個code合併成一個code
  20.         tmp.l = &x[0] ;
  21.         tmp.r = &x[1] ;
  22.         tmp.isLeaf = false ;

  23.         Huffman *t = new Huffman[size - 1] ;        //建一個比input還要小一個的空間
  24.         int i ;
  25.         for ( i = 2 ; i < size  ; i++ )                        //把後面的code放入t
  26.                 t[i - 2 ] = x[i] ;
  27.         t[i-2] = tmp ;                                                        //最後把合併的code放入t的最後一個

  28.         return MergerCode ( t , size - 1) ;                //遞迴呼叫自己的函式
  29. }
  30. class SimpleSearch {
  31. private:
  32.         Huffman *sQueue , *ptr ;
  33.         bool SelectNode () {
  34.                 if ( ! this->sQueue->isLeaf ){                        //如果現在指向的元素不是樹葉
  35.                         Huffman *t = this->sQueue ;                        //point to current vQueue
  36.                        
  37.                         while ( t->next != NULL )                        //找到sQueue的最後一個指標
  38.                                 t = t->next ;

  39.                         int i = 0 ;
  40.                         char *buffer = new char [50] ;

  41.                         for ( ; i < (int)strlen(this->sQueue->code) ; i++ )                        //把目前的節點的code記錄到buffer裡面
  42.                                 buffer[i] = this->sQueue->code[i] ;

  43.                         buffer[i] = '\0' ;

  44.                         t->next = this->sQueue->l ;                                                                        //把目前的節點的左子樹加入sQueue最後
  45.                         t->next->code = new char [50] ;

  46.                         for ( i = 0 ; i < (int) strlen ( buffer ) ; i++ )                       
  47.                                 t->next->code [i] = buffer[i] ;
  48.                         t->next->code[i++] = '0' ;
  49.                         t->next->code[i] = '\0' ;
  50.                         t = t->next ;

  51.                         t->next = this->sQueue->r ;                                                                        //把目前的節點的右子樹加入sQueue最後
  52.                         t->next->code = new char [50] ;

  53.                         for( i = 0 ; i < (int) strlen( buffer ) ; i++ )
  54.                                 t->next->code[i] = buffer[i] ;
  55.                         t->next->code[i++] = '1' ;
  56.                         t->next->code[i] = '\0' ;
  57.                        
  58.                         t->next->next = NULL ;

  59.                         delete []buffer ;
  60.                 }

  61.                 this->sQueue = this->sQueue->next ;                                                                //指向下一個code
  62.                 if ( this->sQueue == NULL )
  63.                         return false ;
  64.                 return true ;
  65.         } ;
  66. public:
  67.         Huffman *first ;

  68.         SimpleSearch(Huffman *root ) {
  69.                 this->first = new Huffman() ;
  70.                 this->ptr = first ;

  71.                 this->sQueue = root ;
  72.                 this->sQueue->code = new char [50] ;
  73.                 this->sQueue->code[0] = '\0' ;
  74.                 this->sQueue->next = NULL ;
  75.         } ;
  76.         void BFS( ) {
  77.                 if ( !SelectNode( ) ) {
  78.                         return ;
  79.                 } else {
  80.                         if ( this->sQueue->ch != NULL ) {                        //如果目前的.ch 不是NULL
  81.                                 ptr->next = this->sQueue ;                                //把目前的節點加入ptr後面
  82.                                 ptr = ptr->next ;
  83.                         }
  84.                         BFS() ;
  85.                 }
  86.         } ;
  87. } ;
  88. void InsertionSort ( Huffman *x , int size ) {
  89.         for ( int i = 1 ; i < size ; i++ ) {
  90.                 int j = i ;
  91.                 while ( j > 0 ) {
  92.                         if ( x[j].times < x[j-1].times )
  93.                                 swap(x[j] , x[j-1] ) ;
  94.                         else if ( x[j].times == x[j-1].times ) {
  95.                                 if ( x[j].ch < x[j-1].ch )
  96.                                         swap(x[j] , x[j-1] ) ;
  97.                         }
  98.                         j -= 1;
  99.                 }
  100.         }
  101. }
  102. int main() {
  103.         char input[80] ;
  104.         cin.getline(input , 80 , '\n') ;

  105.         cout << "input-> " << input << endl ;

  106.         Huffman Codes[80] ;
  107.         int i = 0 , ammount = 0 ;
  108.        
  109.         //把輸入字元做統計
  110.         while ( input[i] != '\0' ) {                       
  111.                 bool flag = false ;
  112.                 for ( int j = 0 ; j < ammount ; j++ ){
  113.                         if( Codes[j].ch == input[i] ) {
  114.                                 flag = true ; Codes[j].times ++ ; break ;
  115.                         }
  116.                 }

  117.                 if( !flag ) {
  118.                         Codes[ammount].isLeaf = true ;
  119.                         Codes[ammount].l = NULL ;
  120.                         Codes[ammount].r = NULL ;
  121.                         Codes[ammount].ch = input[i] ;
  122.                         Codes[ammount++].times = 1 ;
  123.                        
  124.                 }
  125.                 i++ ;
  126.         }

  127.         Huffman *t = MergerCode( Codes , ammount) ;                //合併codes

  128.         SimpleSearch x(t) ;                                                               
  129.         x.BFS() ;                                                                                //走訪每個節點
  130.         i = 0 ;
  131.         //輸出coding的結果

  132.         cout << "After encoding->" ;
  133.         while ( input[i] != '\0' ) {
  134.                 //查表
  135.                 Huffman *t = x.first->next ;
  136.                 while (t != NULL ) {
  137.                         if ( t->ch == input[i] ) {
  138.                                 cout << t->code << "|" ; break ;
  139.                         }
  140.                         t = t->next ;
  141.                 }
  142.                 i++ ;
  143.         }

  144.         cout << endl ;

  145.         int s = 0 ;
  146.         //統計總共要多少位元數
  147.         while( x.first != NULL ) {
  148.                 if (x.first->ch != NULL )
  149.                         s += x.first->times * (int) strlen(x.first->code) ;
  150.                 x.first = x.first->next ;
  151.         }
  152.         cout << "Total bits->" << s << endl ;
  153.         system("pause") ;
  154.         return 0 ;
  155. }
複製代碼
裡面寫了linked list & tree結構,兩個寫在同一個struct裡面
Complier: vc++ 2008
剛剛試了一下輸入 / 輸出是OK的,給你參考看看!!...
瀏覽完整內容,請先 註冊登入會員
若瀏覽伊莉的時侯發生問題或不正常情況,請使用Internet Explorer(I.E)。

使用道具檢舉

您需要登錄後才可以回帖 登錄 | 註冊

Powered by Discuz!

© Comsenz Inc.

重要聲明:本討論區是以即時上載留言的方式運作,對所有留言的真實性、完整性及立場等,不負任何法律責任。而一切留言之言論只代表留言者個人意見,並非本網站之立場,用戶不應信賴內容,並應自行判斷內容之真實性。於有關情形下,用戶應尋求專業意見(如涉及醫療、法律或投資等問題)。 由於本討論區受到「即時上載留言」運作方式所規限,故不能完全監察所有留言,若讀者發現有留言出現問題,請聯絡我們。有權刪除任何留言及拒絕任何人士上載留言,同時亦有不刪除留言的權利。切勿上傳和撰寫 侵犯版權(未經授權)、粗言穢語、誹謗、渲染色情暴力或人身攻擊的言論,敬請自律。本網站保留一切法律權利。
回頂部