Babelfish POJ - 2503 (hash)

版权声明: https://blog.csdn.net/nucleare/article/details/89300549

Babelfish

 POJ - 2503 

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.

#include <iostream>
#include <cstring>
#include <algorithm>
#include <string> 
#include <cstdio>
using namespace std;
#define ull unsigned long long
const int N = 1e5 + 7;
const int P = 1e9 + 7 + 10; //选择大质数(至少大于字符串长度 
int head[N+10], en;
struct Edge {
	int v ,to;
} e[N+N];
int hash(char *s) {
	ull m = 0; //ull 自然溢出==mod 
	for (int i = 1; s[i]; ++i) {
		m = m * P + s[i];
	}
	return m % N + 1;
}
void adde(ull u, int v) { //解决冲突 
	e[++en].v = v;
	e[en].to = head[u]; 
	head[u] = en;
}
char s1[100008][12], s2[100008][12], s[107];
int main() {
	int tot = 0;
	memset(head, -1, sizeof(head)); en = 0;
	while (gets(s) && s[0] != '\0') {
		++tot;
		sscanf (s, "%s %s", s2[tot]+1, s1[tot]+1);
		adde(hash(s1[tot]), tot);
	}
	int f;
	while (gets(s+1)) {
		f = 0;
		for (int i = head[hash(s)]; ~i; i = e[i].to) {
			int v = e[i].v;
			if (strcmp(s1[v]+1, s+1) == 0) {
				printf ("%s\n", s2[v]+1);
				f = 1;
				break;
			}
		}
		if (!f) {
			puts("eh");
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/nucleare/article/details/89300549
今日推荐