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

            導航

            <2011年5月>
            24252627282930
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            統計

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久久久久久尹人综合网亚洲| 中文字幕乱码人妻无码久久| 99久久www免费人成精品| 久久精品亚洲精品国产欧美| 中文字幕无码久久人妻| 国产精品久久久久aaaa| 亚洲国产成人久久综合一区77| 久久精品亚洲一区二区三区浴池| 国内精品久久久久久久亚洲| 国内精品伊人久久久久777| 18岁日韩内射颜射午夜久久成人| 中文成人无码精品久久久不卡 | 中文国产成人精品久久不卡| 国产精品99久久久久久人| 亚洲人成电影网站久久| 久久精品aⅴ无码中文字字幕不卡| 久久精品亚洲福利| 伊人色综合久久天天| 久久精品国产网红主播| 狠狠色婷婷久久一区二区| 久久免费99精品国产自在现线 | 久久国产精品成人免费| 久久精品国产乱子伦| 亚洲国产香蕉人人爽成AV片久久| 久久精品免费观看| 久久国产精品成人免费| 99久久99久久久精品齐齐| 午夜欧美精品久久久久久久| 久久99热这里只有精品66| 久久久WWW免费人成精品| 国产成人久久精品二区三区| 国产69精品久久久久99尤物| 久久精品无码一区二区三区| 激情伊人五月天久久综合| 久久天天躁狠狠躁夜夜网站| 无码人妻久久一区二区三区 | 天天影视色香欲综合久久| 久久精品国产国产精品四凭| 久久久久亚洲?V成人无码| 天天综合久久一二三区| 精品多毛少妇人妻AV免费久久 |