Babelfish
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 41750 | Accepted: 17700 |
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
Hint
Huge input and output,scanf and printf are recommended.
查词典题,不过我因为把手误把 int 打成 char WA了几次。。
字符串哈希用ELFHash会快上很多,至少是我自己写的哈希的两倍快。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <bitset>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#define FOP freopen("data.txt","r",stdin)
#define inf 0x3f3f3f3f
#define maxn 1000010
#define mod 1000007
#define PI acos(-1.0)
#define LL long long
using namespace std;
struct Node
{
int pos;
int next;
}node[maxn];
int cot;
int hashTable[maxn];
int n = 0;
char word[100010][12];
char vals[100010][12];
void initHash()
{
cot = 0;
memset(hashTable, -1, sizeof(hashTable));
}
bool cmp(char *a, char *b)
{
return strcmp(a, b) == 0;
}
int getHash(char *a)
{
int res = 0;
for(int i = 0; a[i] != '\0'; i++) res += a[i] * (i+1);
return res % mod;
}
int ELFHash(char *key)
{
unsigned long h = 0;
while(*key)
{
h = (h<<4) + (*key++);
unsigned long g = h&0Xf0000000L;
if(g) h ^= g>>24;
h &= ~g;
}
return h%mod;
}
void insertHash(char *val, int pos)
{
node[cot].pos = pos;
//int key = getHash(val);
int key = ELFHash(val);
node[cot].next = hashTable[key];
hashTable[key] = cot++;
}
int searchHash(char *val)
{
//int key = getHash(val);
int key = ELFHash(val);
int next = hashTable[key];
while(next != -1)
{
int t = node[next].pos;
if(cmp(val, vals[t])) return t;
next = node[next].next;
}
return -1;
}
int main()
{
//FOP;
initHash();
char c[100];
char val[12];
while(gets(c) && c[0] != '\0')
{
sscanf(c, "%s %s", word[n], vals[n]);
insertHash(vals[n], n);
n++;
}
while(~scanf("%s", val))
{
int t = searchHash(val);
if(t != -1) printf("%s\n", word[t]);
else printf("eh\n");
}
return 0;
}