• <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)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            国产午夜久久影院| 大香伊人久久精品一区二区| 97久久精品无码一区二区天美| 九九久久自然熟的香蕉图片| 免费国产99久久久香蕉| 久久亚洲电影| 99久久99久久精品免费看蜜桃| 久久九九久精品国产| 7777精品久久久大香线蕉| 国产精品免费久久| 伊人久久大香线焦AV综合影院| 88久久精品无码一区二区毛片 | 九九精品99久久久香蕉| 久久人人超碰精品CAOPOREN| 久久久av波多野一区二区| 一本色道久久综合狠狠躁篇| 国产精品99久久久久久www| 色欲综合久久躁天天躁蜜桃| 四虎国产精品成人免费久久| 老司机国内精品久久久久| 久久久久久夜精品精品免费啦| 女同久久| 亚洲欧洲久久av| 欧美精品丝袜久久久中文字幕 | 久久夜色精品国产噜噜噜亚洲AV| 久久丝袜精品中文字幕| 99热热久久这里只有精品68| 国产综合久久久久| 久久精品毛片免费观看| 久久婷婷成人综合色综合| 亚洲欧美日韩久久精品第一区 | 国产午夜久久影院| 高清免费久久午夜精品| 精品久久久久久亚洲| 97久久精品无码一区二区| 99国产精品久久| 久久国产免费观看精品| 91久久香蕉国产熟女线看| 中文字幕亚洲综合久久| 久久精品国产WWW456C0M| 午夜精品久久久久久久无码|