zoukankan      html  css  js  c++  java
  • Letters比赛第六场1002 Babelfish解题报告

    1002 Babelfish (POJ 2503)

    解题思路:字符串的哈希,找一个比较好的hash函数就可以了,冲突时用链表的形式组织。用STL中的map等容器也可以过,不过性能差点。

     

    代码如下:

    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    #define N 1000005
    #define HASH 3999971
    
    struct node
    {
        char a[11];
        char b[11];
        int next;
    };
    
    struct node words[N];
    int head[HASH];
    int t = 0;
    
    int inline hash(char * key)
    {
        unsigned int h = 0;
        while (*key){
            h = (h << 4) + *key++;
            unsigned int g = h & 0xf0000000L;
            if (g) h ^= g >> 24;
            h &= ~g;
        }
        return h % HASH;
    }
    
    bool search(char s[])
    {
        int key = hash(s);
        int u = head[key];
    
        while(u)
        {
            if(strcmp(s, words[u].b) == 0)
            {
                t = u;
                return true;
            }
            else
            {
                u = words[u].next;
            }
        }
        return false;
    }
    
    int main()
    {
    #ifdef MYLOCAL
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    
        int i = 1;
        char s[30];
    
        memset(head, 0, sizeof(head));
        while(gets(s) && strcmp(s, "") != 0)
        {
            sscanf(s, "%s %s", words[i].a, words[i].b);
            int key = hash(words[i].b);
            words[i].next = head[key];
            head[key] = i++;
        }
    
        while(scanf("%s", s) != EOF)
        {
            if(search(s) == true)
            {
                printf("%s\n", words[t].a);
            }
            else
            {
                printf("eh\n");
            }
        }
    
        return 0;
    }
  • 相关阅读:
    python pyinotify模块详解
    lastpass密码管理工具使用教程
    MAMP 环境下安装Redis扩展
    SourceTree使用方法
    Mac securecrt 破解
    Memcache 安装
    Warning: setcookie() expects parameter 3 to be long, string given
    SQLSTATE[HY000] [2002] Connection refused
    插件管理无法访问
    光栅化渲染器
  • 原文地址:https://www.cnblogs.com/LETTers/p/2468967.html
Copyright © 2011-2022 走看看