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

            導航

            <2010年8月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            統(tǒng)計

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            精品无码久久久久国产动漫3d| 丁香色欲久久久久久综合网| 国产成人久久精品麻豆一区| 99久久精品免费看国产| 欧美麻豆久久久久久中文| 麻豆一区二区99久久久久| 日本三级久久网| 18岁日韩内射颜射午夜久久成人 | 久久国产精品国产自线拍免费 | 久久久久久久久久久| 91久久香蕉国产熟女线看| 久久久亚洲欧洲日产国码是AV| 久久精品国产91久久麻豆自制| 久久综合鬼色88久久精品综合自在自线噜噜 | 狠狠色综合网站久久久久久久 | 国产亚洲欧美精品久久久| 一本久久免费视频| 9999国产精品欧美久久久久久| 综合久久国产九一剧情麻豆| 欧美伊人久久大香线蕉综合69 | 久久久久久久久久免免费精品 | 97精品伊人久久大香线蕉app| 久久伊人精品一区二区三区| 久久五月精品中文字幕| 91精品国产高清久久久久久国产嫩草| 日韩人妻无码一区二区三区久久| 97精品伊人久久大香线蕉| 一本久久a久久精品综合香蕉| 少妇久久久久久被弄到高潮 | 婷婷久久精品国产| 久久午夜综合久久| 久久久黄片| 亚洲国产成人久久综合区| 超级97碰碰碰碰久久久久最新| 亚洲乱码日产精品a级毛片久久| 人人狠狠综合久久亚洲高清| 色天使久久综合网天天| 亚洲乱码精品久久久久..| 久久精品人人做人人爽电影蜜月 | 91久久精品无码一区二区毛片| 日韩精品国产自在久久现线拍|