Babelfish

题目链接

  • 简单的字符串HASH题目,关键学习一下黑书的字符串elfhash
const int MAXN = 100010;

const int maxn=1000000;
const int maxm=100003;
const int maxlen=20;

class hash
{
    private:
    struct node{
        char ch[maxlen];
        int ptr;
        node* next;
    };
    node *h[maxm],s[maxn];
    int numptr;
    int Hash(char *key)
    {
        unsigned long h=0,g;
        while(*key)
        {
            h=(h<<4)+*key++;
            g=h&0xf0000000L;
            if(g)h^=g>>24;
            h&=~g;
        }
        return h%maxm;
    }
    public:
    void init()
    {
        int i;
        for(i=0;i<maxm;i++)h[i]=NULL;
        numptr=0;
    }
    int ins(char ch[])
    {
        int temp=Hash(ch);
        strcpy(s[numptr].ch,ch);
        s[numptr].next=h[temp];
        s[numptr].ptr=numptr;
        h[temp]=&s[numptr];
        numptr++;
        return numptr-1;
    }
    int question(char ch[])
    {
        int temp=Hash(ch);
        node* ptr=h[temp];
        while(ptr)
        {
            if(strcmp(ptr->ch,ch)==0)
            {
                return ptr->ptr;
            }
            ptr=ptr->next;
        }
        return -1;
    }
} hash;

char ipt[30], a[20], b[20];
char ans[MAXN][20];

int main ()
{
    hash.init();
    while (gets(ipt) && ipt[0] != 0)
    {
        sscanf(ipt, "%s %s", a, b);
        int hs = hash.ins(b);
        strcpy(ans[hs], a);
    }
    while (gets(ipt) && ipt[0] != 0)
    {
        int hs = hash.question(ipt);
        if (hs != -1)
            printf("%s\n", ans[hs]);
        else
            puts("eh");
    }
    return 0;
}
/*
dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay
Sample Output

cat
eh
loops
*/

Babelfish

时间: 2024-10-13 22:19:10

Babelfish的相关文章

POJ 2503 Babelfish (Trie树 或 map)

Babelfish Time Limit: 3000MS        Memory Limit: 65536K Total Submissions: 34278        Accepted: 14706 Description You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately

POJ2503——Babelfish(map映射+string字符串)

Babelfish DescriptionYou have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.InputInput consists of up to 100,000 diction

Kattis - Babelfish

Babelfish You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them. Input Input consists of up to 100000100000 dictionary

Babelfish(二分)

Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 37238   Accepted: 15879 Description You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have

POJ 2503 Babelfish(字典树)

Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 35009   Accepted: 14979 Description You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have

POJ2503:Babelfish(二分)

Description You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them. Input Input consists of up to 100,000 dictionary ent

poj 2503:Babelfish(字典树,经典题,字典翻译)

Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 30816   Accepted: 13283 Description You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have

POJ2503 Babelfish

问题链接:POJ2503 Babelfish. 这个问题只是一个字典问题,自然用map来实现.问题的关键是时间上能否更快. 本来是想用类unordered_map(采用哈希搜索的map)来编写程序,编译不支持,只好改为map. 这个问题用类unordered_map来编写程序,时间上会更快一些,也更为合理. AC通过程序如下: /* POJ2503 Babelfish */ #include <iostream> #include <string> //#include <u

Babelfish (map 用法。&lt;string, string&gt;

Babelfish - You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them. Input Input consists of up to 100,000 dictionary ent

poj2503(Babelfish)

题目地址:Babelfish 题目大意: 你将要迁徙一个大城市里,但是你们的语言不一样.但是幸运的是你有一本字典,通过你的字典可以翻译外国的语言.每一行先是字典的单词接着是外国语言.  字典输完,给你几个外国语言,输出字典的单词否则输出“eh”. 解题思路: map将每一行的单词mp一个整数,这整数可以是序列号,这样你找到对应的mp数值就很容易输出.注意输入输出即可. 代码: 1 #include <algorithm> 2 #include <iostream> 3 #inclu