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
*/