• <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)論排行榜

            久久久久久无码国产精品中文字幕| 久久综合五月丁香久久激情| 国产人久久人人人人爽| 久久99久久99小草精品免视看| 久久久久亚洲精品男人的天堂| 亚洲AV无码1区2区久久| 国产精品久久久久久一区二区三区| 久久久久亚洲精品中文字幕| 久久久久亚洲AV无码网站| 麻豆久久| 国产AV影片久久久久久| 亚洲伊人久久大香线蕉综合图片| 国产高潮久久免费观看| 久久精品亚洲日本波多野结衣| 久久国产视屏| 日本免费久久久久久久网站 | 久久国产精品一区二区| 亚洲国产综合久久天堂 | 久久久久亚洲AV片无码下载蜜桃 | 狠狠狠色丁香婷婷综合久久五月 | 精品久久久久久无码中文字幕| 亚洲国产另类久久久精品小说| 亚洲国产精品无码久久青草| 国产91久久综合| 日本久久久精品中文字幕| 国产欧美一区二区久久| 久久精品国产久精国产思思| 久久久国产乱子伦精品作者| 久久精品中文无码资源站| 久久精品综合网| 国产亚洲精久久久久久无码77777 国产亚洲精品久久久久秋霞 | 久久久久高潮综合影院| 婷婷久久综合九色综合九七| 久久久久亚洲AV无码专区网站| 久久精品国产一区二区电影| 国产精品va久久久久久久| 国产精品视频久久久| 91精品国产高清久久久久久91| 国产成人久久精品二区三区| 岛国搬运www久久| 亚洲国产精品无码久久九九 |