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

            導航

            <2010年10月>
            262728293012
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            統計

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            中文成人无码精品久久久不卡| 日日狠狠久久偷偷色综合96蜜桃| 久久婷婷色香五月综合激情 | 久久笫一福利免费导航 | 久久精品一区二区国产| 久久综合久久综合久久| 久久本道综合久久伊人| 伊人伊成久久人综合网777| 亚洲中文精品久久久久久不卡| 97久久精品人妻人人搡人人玩| 久久久久久久99精品免费观看| 久久天天日天天操综合伊人av| 偷偷做久久久久网站| 久久国产精品99国产精| 日本精品一区二区久久久| 少妇人妻88久久中文字幕| 久久99精品久久久久久噜噜| 国产A三级久久精品| 精品久久久久久久久久中文字幕 | 热久久视久久精品18| 日本精品久久久久中文字幕8| 四虎影视久久久免费| 国产一级持黄大片99久久| 99久久精品免费看国产一区二区三区| 国产精品久久久久影院色| 亚洲精品国精品久久99热一| 久久久久亚洲精品无码网址| 久久最新精品国产| 97久久精品人妻人人搡人人玩| A级毛片无码久久精品免费| 人人狠狠综合久久亚洲高清| 国产精品热久久无码av| 国产精品一久久香蕉国产线看| 人妻少妇久久中文字幕| 伊人久久大香线蕉综合Av| 日韩十八禁一区二区久久| 久久久久亚洲精品男人的天堂| 国产精品99久久久久久董美香| 日本免费久久久久久久网站| 久久99热精品| 四虎国产永久免费久久|