poj 2503 Babelfish(hash or 字典树)

题目链接

Babelfish
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 35080 Accepted: 15013

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 entries, followed by a blank line, followed by a message of up to 100,000 words. Each dictionary entry is a line containing an English word, followed by a space and a foreign language word. No foreign word appears more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.

Output

Output is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as "eh".

Sample Input

dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay

Sample Output

cat
eh
loops

题解:直接用字符串hash,或者字典树。

代码如下:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<set>
#include<vector>
#include<string.h>
#include<map>
#define inff 0x3fffffff
#define nn 1100000
#define mod 1000007
typedef __int64 LL;
typedef unsigned __int64 LLU;
using namespace std;
char s[110];
bool use[nn];
LLU ha[nn];
string ans[nn];
void add(string x,string y)
{
    LLU ix=0;
    for(int i=0;i<(int)x.size();i++)
    {
        ix=ix*131+x[i];
    }
    int wei=ix%mod;
    for(int i=wei;;i=(i+1)%mod)
    {
        if(!use[i])
        {
            use[i]=true;
            ha[i]=ix;
            ans[i]=y;
            break;
        }
    }
}
string Hash(string x)
{
    LLU ix=0;
    for(int i=0;i<(int)x.size();i++)
    {
        ix=ix*131+x[i];
    }
    int wei=ix%mod;
    for(int i=wei;;i=(i+1)%mod)
    {
        if(!use[i])
        {
            return "eh";
        }
        else if(ha[i]==ix)
            return ans[i];
    }
    return "eh";
}
int main()
{
    int i;
    bool ok=true;
    memset(use,false,sizeof(use));
    while(gets(s))
    {
        int l=strlen(s);
        if(l==0)
        {
            ok=false;
            continue;
        }
        if(ok)
        {
            string ix="";
            string en,fo;
            for(i=0;i<l;i++)
            {
                if(s[i]==' ')
                {
                    en=ix;
                    ix="";
                }
                else
                    ix+=s[i];
            }
            fo=ix;
            add(fo,en);
        }
        else
        {
            string ix=s;
            printf("%s\n",Hash(ix).c_str());
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值