• <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)  編輯 收藏 引用
            久久精品无码午夜福利理论片 | 日韩精品国产自在久久现线拍 | 久久综合综合久久97色| 精品一区二区久久| 日本久久中文字幕| 久久夜色精品国产噜噜噜亚洲AV| 99久久免费国产精精品| 久久精品亚洲男人的天堂| 久久综合国产乱子伦精品免费| 久久精品国产69国产精品亚洲| 青青久久精品国产免费看| 久久精品中文闷骚内射| 日本亚洲色大成网站WWW久久| 人妻无码αv中文字幕久久琪琪布| 国产精品99久久久久久猫咪| 日本五月天婷久久网站| 免费精品99久久国产综合精品| 久久精品国产清自在天天线| 91超碰碰碰碰久久久久久综合| 伊人久久精品无码二区麻豆| 久久久久亚洲精品男人的天堂| 成人资源影音先锋久久资源网| 伊人久久一区二区三区无码| 国产精品无码久久四虎| 久久久久99精品成人片试看| 香蕉久久夜色精品国产尤物| 韩国三级中文字幕hd久久精品| 久久久精品人妻一区二区三区蜜桃| 国产精品久久婷婷六月丁香| 青青草国产97免久久费观看| 国产精品欧美亚洲韩国日本久久 | 色老头网站久久网| 久久精品国产福利国产琪琪| 亚洲天堂久久精品| 久久精品九九亚洲精品天堂| 色偷偷久久一区二区三区| 久久精品中文无码资源站| 久久婷婷五月综合97色直播| 18禁黄久久久AAA片| 精品久久亚洲中文无码| 无码人妻久久久一区二区三区|