• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            #include <cstdio>
            #include 
            <algorithm>
            #include 
            <vector>
            #include 
            <queue>
            #include 
            <string>

            using namespace std;

            struct HTNode
            {
                
            int weight, letter, lchild, rchild;
                
                HTNode(){}
                HTNode( 
            int a, int b, int c, int d ) :weight(a), letter(b), lchild(c), rchild(d) {}
            };

            struct  Info
            {
                
            int  num, lett, id;
                Info();
                Info( 
            int a, int c,  int b ): num(a), lett(c), id(b) {}
            };

            bool operator< ( Info const& a, Info const& b )
            {
                
            return a.num> b.num;
            }

            int     num[26], len= 0, root;
            char    str[1000];
            string  code[26];
            vector
            <HTNode>  ht;
            priority_queue
            <Info>  info;

            void buildtree();
            void codeht(int,string);
            void decode( int,int);

            void input()                                  //  輸入過程 
            {
                printf(
            "注意:  該程序只處理大寫字母, 其它字符不予處理\n");
                printf(
            "請輸入文本, 在最后另起一行按  F6  結束輸入:\n");
                
            char ch;
                
                memset( num, 
            0sizeof(num) );
                
            while( scanf("%c",&ch)!= EOF )
                    
            if( ch>= 'A' && ch<= 'Z' )  num[ch- 'A']++;
                    
                printf(
            "各字母個數為:\n");
                
            for(int i= 0; i< 26++i )
                
            if( num[i] ) printf("%c  %d\n", i+ 'A', num[i] ); 
            }

            void test()
            {
                input();
                buildtree();
                codeht( root, 
            "" );
                
                printf(
            "各字母的哈夫曼編碼為:\n\n"); 
                
            forint i= 0; i< 26++i )
                
            if( num[i] ) printf("%c  %s\n", i+ 'A', code[i].c_str() );
                
                printf(
            "\n\n\n請輸入你要解碼的字符串,不能有空格,并且輸入合法\n"); 
                scanf(
            "%s", str );
                
                printf(
            "解碼后為:\n\n"); 
                
                decode(
            0, root ); 
                printf(
            "\n\n\n");
            }

            void codeht( int i, string s )                    //   編碼過程 
            {
                
            if( ht[i].lchild== -1 )  
                {
                    code[ ht[i].letter ]
            = s;
                }
                
            else
                {
                    codeht( ht[i].lchild, s
            + '0' );
                    codeht( ht[i].rchild, s
            + '1' );
                }
            }

            void decode( int i, int j )                      //   解碼過程 
            {
                
            if( ht[j].lchild== -1 && ht[j].rchild== -1 )
                {
                    printf(
            "%c", ht[j].letter+ 'A' );
                    
            if( str[i] ) decode( i, root );
                }
                
            else if!str[i] ) return;
                
            else if( str[i]== '0' )      decode( i+ 1, ht[j].lchild );
                
            else if( str[i]== '1' )      decode( i+ 1, ht[j].rchild );
            }

            void buildtree()                                //   建樹過程 
            {
                len
            = 0;
                
            forint i= 0; i< 26++i )
                
            if( num[i] )
                {
                    ht.push_back( HTNode( num[i], i, 
            -1-1 ) );
                    info.push( Info( num[i], i, len
            ++ ) );
                }
                
                
            if( info.size()== 1 ) {  root= 0;  return;  }
                
                Info  a
            = info.top();  info.pop();
                Info  b
            = info.top();  info.pop();
                
                info.push( Info( a.num
            + b.num, -1, len++ ) );
                ht.push_back( HTNode( a.num
            + b.num, -1, a.id, b.id ) );
                
                
            while( info.size()> 1 )
                {
                    Info a
            = info.top(); info.pop();
                    Info b
            = info.top(); info.pop();
                    
                    info.push( Info( a.num
            + b.num, -1, len++ ) );
                    ht.push_back( HTNode( a.num
            + b.num, -1, a.id, b.id ) );
                }    
                
                root
            = info.top().id;
            }

            int main()
            {
                test();
                
                system(
            "pause");
                
            return 0;
            }

            /*

            7 5 2 4 6

            */
            posted on 2008-11-12 21:03 Darren 閱讀(293) 評論(0)  編輯 收藏 引用
            一本一道久久精品综合| 99久久精品国产一区二区| 伊人色综合久久天天网| 国产aⅴ激情无码久久| 99久久99久久精品国产片果冻| 国产成人精品三上悠亚久久| 1000部精品久久久久久久久| 国产精品女同一区二区久久| 色婷婷综合久久久中文字幕| 国产成人久久精品二区三区| 久久天天躁狠狠躁夜夜不卡| 亚洲天堂久久精品| 国产99久久久国产精品小说| 久久久久四虎国产精品| 亚洲中文字幕无码久久2017| 国产午夜福利精品久久| 久久精品国产精品亚洲精品 | 久久天天婷婷五月俺也去| 久久精品国产亚洲AV影院| 国产精品九九久久免费视频 | 久久综合色区| 久久精品嫩草影院| 久久国产免费直播| 欧美成a人片免费看久久| 色噜噜狠狠先锋影音久久| 久久久久99精品成人片试看| 久久婷婷午色综合夜啪| 美女久久久久久| 精品乱码久久久久久夜夜嗨| 久久精品国产亚洲麻豆| 精品久久一区二区| 999久久久免费精品国产| 国产69精品久久久久久人妻精品| 久久中文精品无码中文字幕| 国产午夜精品久久久久九九| 久久免费精品视频| 久久久综合九色合综国产| 色综合久久天天综合| 国内精品久久久久久不卡影院| 91亚洲国产成人久久精品网址 | 亚洲午夜久久久久久噜噜噜|