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

            堅(jiān)信:勤能補(bǔ)拙

            2011字符串哈希, 統(tǒng)計(jì)詞頻(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 閱讀(590) 評(píng)論(0)  編輯 收藏 引用 所屬分類(lèi): R_找工復(fù)習(xí)2011

            導(dǎo)航

            <2025年8月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            統(tǒng)計(jì)

            常用鏈接

            留言簿(1)

            隨筆分類(lèi)

            隨筆檔案

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            理论片午午伦夜理片久久 | 久久精品国产只有精品66| 久久久无码一区二区三区| 久久久噜噜噜www成人网| 日本精品久久久久中文字幕8| 久久精品国产一区二区三区日韩| 伊人热人久久中文字幕| 久久受www免费人成_看片中文| 久久久久久国产a免费观看黄色大片 | 人妻无码αv中文字幕久久琪琪布| 久久精品人妻中文系列| 99久久亚洲综合精品网站| yy6080久久| 久久久精品无码专区不卡| 久久久久久国产精品无码超碰| 久久国产成人亚洲精品影院| 久久亚洲精精品中文字幕| 亚洲欧洲久久久精品| 久久久久久久久久久精品尤物 | 久久精品成人| 久久久久久久人妻无码中文字幕爆| 久久99精品久久久久久噜噜| 久久综合九色综合精品| 久久精品国产2020| 亚洲国产视频久久| 午夜肉伦伦影院久久精品免费看国产一区二区三区 | 91视频国产91久久久| 精品久久亚洲中文无码| 香蕉99久久国产综合精品宅男自| 国产精品久久成人影院| AV狠狠色丁香婷婷综合久久| 久久青青草原亚洲av无码app| 久久久久se色偷偷亚洲精品av| 一级做a爰片久久毛片看看| 久久九色综合九色99伊人| 久久97久久97精品免视看| 久久99国产精品成人欧美| 久久精品国产只有精品66| 婷婷国产天堂久久综合五月| 久久福利资源国产精品999| 久久精品国产亚洲av麻豆蜜芽|