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

            堅信:勤能補拙

            PKU 2503 Babelfish

            問題:
            http://acm.pku.edu.cn/JudgeOnline/problem?id=2503

            思路:
            字符串哈希
            這里使用的哈希函數(shù)是ELFHash
             1 int ELFHash(char *str)
             2 {
             3     unsigned long t, hash = 0;
             4     while(*str) {
             5         hash = (hash<<4+ (*str++);
             6         if((t = hash&0xF0000000L))
             7             hash ^= t>>24;
             8         hash &= ~t;
             9     }
            10     return (hash & 0x7FFFFFFF)%PRIME;
            11 }

            因為之前寫哈希表解決沖突都是用的鏈接法,這里想嘗試一下開放地址法,采用最簡單的線性探查,結果居然TLE
            無奈還是改成鏈接法,然后就AC了
            不過,時間卻有719MS之多,而網(wǎng)上幾乎相同的代碼只需要230MS左右,不知道是何原因

            代碼:
            TLE的開放地址法
             1 void
             2 insert(int hash_val, int index)
             3 {
             4     if(!hash[hash_val].used) {
             5         hash[hash_val].used = 1;
             6         hash[hash_val].index = index;
             7     } else
             8         insert((hash_val+1)%PRIME, index); /* linear probing */
             9 }
            10 
            11 int
            12 search(char *f_word)
            13 {
            14     int hash_val = ELFHash(f_word);
            15     int i = 0;
            16     while(1) {
            17         if(!hash[hash_val].used || i==PRIME)
            18             break;
            19         if(strcmp(f_word, flg[hash[hash_val].index])==0)
            20             return hash[hash_val].index;
            21         hash_val = (hash_val+1)%PRIME;
            22         ++i;
            23     }
            24     return -1;
            25 }
            26 
            27 void
            28 input_hash()
            29 {
            30     int hash_val, index = 0;
            31     char tmp[MAX_LEN*2+1];
            32     memset(hash, 0sizeof(hash));
            33     while(gets(tmp) && tmp[0]) {
            34         sscanf(tmp, "%s %s", eng[index], flg[index]);
            35         hash_val = ELFHash(flg[index]);
            36         insert(hash_val, index);
            37         ++index;
            38     }
            39 }

            AC的鏈接法
             1 void
             2 insert(int hash_val, int index)
             3 {
             4     struct Node *node = (struct Node *)malloc(sizeof(struct Node));
             5     if(node == NULL) {
             6         fprintf(stderr, "malloc error in: insert\n");
             7         exit(1);
             8     }
             9     node->index = index;
            10     node->next = hash[hash_val];
            11     hash[hash_val] = node;
            12 }
            13 
            14 int
            15 search(char *f_word)
            16 {
            17     int hash_val = ELFHash(f_word);
            18     struct Node *node = hash[hash_val];
            19     while(node != NULL) {
            20         if(strcmp(f_word, flg[node->index]) == 0)
            21             return node->index;
            22         node = node->next;
            23     }
            24     return -1;
            25 }
            26 
            27 void
            28 input_hash()
            29 {
            30     int hash_val, index = 0;
            31     char tmp[MAX_LEN*2+1];
            32     memset(hash, 0sizeof(hash));
            33     while(gets(tmp) && tmp[0]) {
            34         sscanf(tmp, "%s %s", eng[index], flg[index]);
            35         hash_val = ELFHash(flg[index]);
            36         insert(hash_val, index);
            37         ++index;
            38     }
            39 }

            posted on 2010-07-28 11:11 simplyzhao 閱讀(223) 評論(0)  編輯 收藏 引用 所屬分類: G_其他

            導航

            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            統(tǒng)計

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            国产精品一久久香蕉产线看| 国内精品伊人久久久久网站| 久久av无码专区亚洲av桃花岛| 久久久久久久久久久久中文字幕 | 婷婷久久久亚洲欧洲日产国码AV | 久久久久这里只有精品| 久久久久久久波多野结衣高潮| 99精品久久精品一区二区| 免费精品国产日韩热久久| 亚洲∧v久久久无码精品| 香蕉久久夜色精品国产2020| 91精品日韩人妻无码久久不卡| 久久婷婷五月综合色高清| 久久亚洲2019中文字幕| 精品国产乱码久久久久久郑州公司 | 国产成人无码精品久久久久免费| 99久久无色码中文字幕人妻| 亚洲国产精品久久久久婷婷软件| 国内精品综合久久久40p| 日韩十八禁一区二区久久| 中文字幕亚洲综合久久2| 久久亚洲精品中文字幕| 亚洲中文字幕久久精品无码喷水| 日本精品久久久久影院日本| 久久狠狠一本精品综合网| 嫩草影院久久国产精品| 国产99久久精品一区二区| 色欲久久久天天天综合网| 国产偷久久久精品专区| yy6080久久| 精品久久久无码21p发布| 人人妻久久人人澡人人爽人人精品| 久久久久国产亚洲AV麻豆| 久久se精品一区精品二区国产| 国产99久久久久久免费看| 久久精品女人天堂AV麻| 久久精品无码一区二区日韩AV| 国产高清国内精品福利99久久| 久久精品国产亚洲精品2020| 久久精品人人做人人爽电影蜜月| 久久精品无码专区免费青青|