【哈希字符串匹配】UVA 10282 - Babelfish

 UVA 10282 - Babelfish

  • 题意:给出一对单词(英文 外语),以一行空行输入结束【这个好难搞,想了好久】。然后是多组询问,输入外语,让我们输出对应的英语。
  • 思路:直接对外语进行hash,然后用map存<hash, 英语>。mp.find()查找输出即可。
  • 其实个人感觉难点就是怎么输入。www,想了好久嗷。用gets输入,空行会以'\0'存储,判断一下即可。开始用了scanf和cin>>string,都不会读入空行www,哭辽。

这里推荐一个这道题的升级版:HDU 1880 魔咒词典


AC CODE:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define lowbit(x) x & (-x)
#define MID (r + l) >> 1
#define lsn rt << 1
#define rsn rt << 1 | 1
#define Lson lsn, l, mid
#define Rson rsn, mid + 1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxN = 1e5 + 7;
const ull base = 233;
map<ull, string>mp;
char s[40], q[20], c;
int main()
{
    while(gets(s))
    {
        if(s[0] == '\0')
            break;
        int len = strlen(s);
        string str;
        int i;
        for(i = 0; s[i] != ' '; i ++ )
            str += s[i];
        ull Hash = 0;
        for(++ i ; i < len; i ++ )
            Hash = Hash * base + s[i] - 'a' + 1;
        mp[Hash] = str;
    }
    while(~scanf("%s", q))
    {
        int len = strlen(q);
        ull Hash = 0;
        for(int i = 0; i < len; i ++ )
            Hash = Hash * base + q[i] - 'a' + 1;
        map<ull, string>:: iterator it;
        it = mp.find(Hash);
        if(it != mp.end())
            cout << it->second << endl;
        else
            cout << "eh\n";
    }
    return 0;
}
/*
dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay
a abc

atcay
ittenkay
oopslay
abc
 */

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值