• <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 閱讀(581) 評論(0)  編輯 收藏 引用 所屬分類: R_找工復習2011

            導航

            <2011年8月>
            31123456
            78910111213
            14151617181920
            21222324252627
            28293031123
            45678910

            統計

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            国产精品岛国久久久久| 国内精品欧美久久精品| 伊人久久无码中文字幕| 色综合久久无码中文字幕| 97久久久久人妻精品专区| 久久精品国产一区二区三区不卡| 久久丫忘忧草产品| 精品综合久久久久久97超人 | 国产成人无码精品久久久免费| 精品国产综合区久久久久久 | 欧美一区二区久久精品| 国产亚洲色婷婷久久99精品| 狠狠色综合网站久久久久久久| 欧美日韩久久中文字幕| 香港aa三级久久三级| 亚洲国产精品无码久久久不卡 | 久久精品桃花综合| 亚洲天堂久久精品| 久久久久亚洲AV无码专区首JN| 91久久成人免费| 精品久久久久久中文字幕人妻最新| 久久精品国产亚洲AV不卡| 久久综合丁香激情久久| 久久水蜜桃亚洲av无码精品麻豆| 四虎久久影院| 久久亚洲国产成人影院网站| 久久亚洲国产午夜精品理论片| 欧美黑人又粗又大久久久| 蜜臀av性久久久久蜜臀aⅴ麻豆| 久久天天躁夜夜躁狠狠躁2022| 久久99精品国产99久久6| 一级做a爱片久久毛片| 色综合久久精品中文字幕首页 | 久久国产热精品波多野结衣AV| 伊人色综合久久天天人手人婷| 久久人与动人物a级毛片| 亚洲国产精品无码久久久蜜芽| 亚洲精品国精品久久99热一| 蜜臀av性久久久久蜜臀aⅴ麻豆| 久久久亚洲欧洲日产国码二区 | 久久精品国产第一区二区|