• <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>

            A Za, A Za, Fighting...

            堅信:勤能補拙

            2011字符串哈希, 統計詞頻(from 編程珠璣)

            代碼1(STL的map版本)
            #include<iostream>
            #include
            <map>
            #include
            <string>

            using namespace std;

            int
            main(
            int argc, char **argv)
            {
                map
            <stringint> M;
                map
            <stringint>::iterator j;
                
            string t;
                
            while(cin>>t)
                    M[t]
            ++;

                
            for(j=M.begin(); j!=M.end(); ++j)
                    cout
            <<j->first<<"\t"<<j->second<<endl;

                
            return 0;
            }


            代碼2(自己的Hash)
            #include<stdio.h>
            #include
            <stdlib.h>
            #include
            <string.h>
            #define WORD_BUF 128
            #define NHASH 29989 /* prime number just bigger than needed */
            #define MULT 31

            struct HNode {
                
            char *word;
                
            int count;
                
            struct HNode *next;
            };
            struct HNode *Hash[NHASH] = {NULL}; 

            #define NODEGROUP 1000
            struct HNode *nodebuf;
            int nodeleft = 0;

            struct HNode *
            node_alloc()
            {
                
            if(nodeleft == 0) {
                    nodebuf 
            = (struct HNode *)malloc(NODEGROUP * sizeof(struct HNode));
                    nodeleft 
            = NODEGROUP;
                }
                
            --nodeleft;
                
            return (nodebuf++);
            }

            unsigned 
            int
            hash(
            char *str) /* a simple implementation of string-hash, others like ELFHash */
            {
                unsigned 
            int ret = 0;
                
            char *ptr;
                
            for(ptr=str; *ptr; ++ptr)
                    ret 
            = ret * MULT + (*ptr);
                
            return (ret % NHASH);
            }

            void
            insert_hash(
            char *word)
            {
                
            struct HNode *node;
                unsigned 
            int h = hash(word);
                
            for(node=Hash[h]; node!=NULL; node=node->next)
                    
            if(strcmp(node->word, word) == 0) {
                        
            ++(node->count);
                        
            return;
                    }
                
            struct HNode *pend = node_alloc();
                pend
            ->word = strdup(word);
                pend
            ->count = 1;
                pend
            ->next = Hash[h];
                Hash[h] 
            = pend;
            }

            int
            main(
            int argc, char **argv)
            {
                
            char buf[WORD_BUF];
                
            while(scanf("%s", buf) != EOF) {
                    insert_hash(buf);
                }

                
            int i;
                
            struct HNode *node;
                
            for(i=0; i<NHASH; ++i) 
                    
            for(node=Hash[i]; node!=NULL; node=node->next) 
                        printf(
            "%s\t%d\n", node->word, node->count);

                
            return 0;
            }

            posted on 2011-08-19 14:37 simplyzhao 閱讀(575) 評論(0)  編輯 收藏 引用 所屬分類: R_找工復習2011

            導航

            <2010年9月>
            2930311234
            567891011
            12131415161718
            19202122232425
            262728293012
            3456789

            統計

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            国产精品无码久久综合| 亚洲国产成人久久综合野外| 久久婷婷国产综合精品| 久久精品水蜜桃av综合天堂| 岛国搬运www久久| 热99RE久久精品这里都是精品免费| 久久精品国产久精国产一老狼| 久久精品无码专区免费东京热| 久久av高潮av无码av喷吹| 97精品国产97久久久久久免费 | 久久一区二区三区99| 久久精品国产免费观看| 国产精品无码久久久久| 色欲久久久天天天综合网精品| 国产成人综合久久精品尤物| 久久精品亚洲欧美日韩久久| 99久久久国产精品免费无卡顿| 精品久久久久久久久久久久久久久| 人妻无码中文久久久久专区| 久久久久亚洲AV成人网| 久久香蕉一级毛片| 久久亚洲私人国产精品vA| 久久综合久久美利坚合众国| 久久精品无码专区免费| 国产高清国内精品福利99久久| 国产精品久久久久影院嫩草| 亚洲αv久久久噜噜噜噜噜| 模特私拍国产精品久久| 精品国产婷婷久久久| 999久久久免费国产精品播放| 国产精品久久久久久一区二区三区| 久久精品一本到99热免费| 亚洲国产成人久久笫一页| 欧洲国产伦久久久久久久 | 激情综合色综合久久综合| 久久综合九色综合精品| 大伊人青草狠狠久久| 久久免费视频观看| 国产AV影片久久久久久| 久久久久国产精品嫩草影院| 久久亚洲AV永久无码精品|