• <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 閱讀(294) 評論(0)  編輯 收藏 引用
            久久不射电影网| 久久久这里只有精品加勒比| 久久久久人妻精品一区二区三区 | 久久精品国产亚洲av日韩| 久久久免费精品re6| 情人伊人久久综合亚洲| 亚洲婷婷国产精品电影人久久| 久久久www免费人成精品| 久久国产精品成人片免费| 日日狠狠久久偷偷色综合免费| 99久久国产精品免费一区二区| 久久精品国产影库免费看| 亚洲日本va午夜中文字幕久久| 精品乱码久久久久久久| 久久强奷乱码老熟女网站| a级成人毛片久久| 综合网日日天干夜夜久久 | 中文字幕日本人妻久久久免费| 精品久久一区二区三区| 精品久久久久久国产| 国产亚洲成人久久| 青青草国产精品久久久久| 国产精品久久午夜夜伦鲁鲁| 亚洲另类欧美综合久久图片区| 国产精品亚洲综合专区片高清久久久| 久久99热这里只有精品国产| 久久久久久毛片免费看| 欧美激情精品久久久久| 国产韩国精品一区二区三区久久| 99久久国产精品免费一区二区| 人妻少妇精品久久| 亚洲国产小视频精品久久久三级| 国产精品日韩深夜福利久久| 国产毛片久久久久久国产毛片 | 久久www免费人成看片| 久久综合九色欧美综合狠狠| 久久国产综合精品五月天| 久久www免费人成看国产片| 久久久久国色AV免费观看 | 狠狠久久亚洲欧美专区| 国内精品九九久久久精品|