• <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  結(jié)束輸入:\n");
                
            char ch;
                
                memset( num, 
            0sizeof(num) );
                
            while( scanf("%c",&ch)!= EOF )
                    
            if( ch>= 'A' && ch<= 'Z' )  num[ch- 'A']++;
                    
                printf(
            "各字母個數(shù)為:\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)  編輯 收藏 引用

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            久久综合中文字幕| 国产精品久久99| 久久人人添人人爽添人人片牛牛| 久久九九久精品国产免费直播| 色婷婷噜噜久久国产精品12p | 久久99国内精品自在现线| 久久九九亚洲精品| 久久综合久久鬼色| 成人资源影音先锋久久资源网| 久久综合一区二区无码| 亚洲va久久久噜噜噜久久| 国产巨作麻豆欧美亚洲综合久久 | 久久久精品一区二区三区| 久久一本综合| 伊人色综合久久| 成人午夜精品无码区久久| 国产L精品国产亚洲区久久| 久久综合国产乱子伦精品免费| 色综合久久88色综合天天 | 色88久久久久高潮综合影院| 国产精品久久新婚兰兰| 久久精品免费大片国产大片| 久久91精品国产91久久户| 亚洲午夜久久久影院伊人| 亚洲另类欧美综合久久图片区| 亚洲综合婷婷久久| 国产精品久久久久久久久免费| 久久大香香蕉国产| 日产精品久久久久久久性色| 超级碰碰碰碰97久久久久| 武侠古典久久婷婷狼人伊人| 久久婷婷五月综合色99啪ak| 久久久久18| 亚洲精品无码久久久| 97视频久久久| 亚洲成色WWW久久网站| 亚洲精品国产字幕久久不卡 | 日韩精品久久久久久久电影蜜臀| 久久午夜无码鲁丝片秋霞| 欧美日韩精品久久免费| 久久久精品国产|