• <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字符串哈希, 統(tǒng)計詞頻(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_找工復(fù)習(xí)2011

            導(dǎo)航

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

            統(tǒng)計

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久精品国产亚洲AV电影| 久久久WWW成人免费精品| 久久人人爽人人人人爽AV| 久久有码中文字幕| 国产成人久久激情91| 国产 亚洲 欧美 另类 久久| 国产精品久久久久乳精品爆| 久久精品国产亚洲Aⅴ蜜臀色欲| 亚洲国产精品嫩草影院久久| 一本色道久久88—综合亚洲精品| 99久久精品费精品国产一区二区| 久久人搡人人玩人妻精品首页| 亚洲色婷婷综合久久| 欧洲性大片xxxxx久久久| 国产精品久久成人影院| 亚洲AV日韩精品久久久久久| 欧美成人免费观看久久| 午夜肉伦伦影院久久精品免费看国产一区二区三区 | 久久综合狠狠综合久久| 久久亚洲国产成人影院网站| 久久久久久亚洲精品成人| 伊人久久大香线蕉综合Av| 2021国产精品久久精品| 久久综合给合久久狠狠狠97色| 欧美久久久久久| 国内精品伊人久久久久AV影院| 中文字幕乱码久久午夜| 999久久久免费精品国产| 国内精品久久久久久中文字幕| 日韩电影久久久被窝网| 国内精品伊人久久久久妇| 无码国内精品久久人妻| 久久精品国产亚洲麻豆| 三级片免费观看久久| 久久99国产精品久久| 亚洲国产精品无码久久青草| 久久精品天天中文字幕人妻| 国产巨作麻豆欧美亚洲综合久久 | 草草久久久无码国产专区| 久久人妻少妇嫩草AV无码蜜桃| 久久精品国产亚洲77777|