Babelfish
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 35754 | Accepted: 15284 |
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.
Source
给出一个对应关系 求出下一个是什么
可以map
可以trie
可以hash
。。。。
好像长度不是10?。。。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define Max 100010
#define MOD 100007
int hashtable[Max];
struct Node
{
char from[23],to[23];
int next;
};
Node p[Max];
char k;
int gethash(char a[])
{
int len=strlen(a);
int hash=5381;
for(int i=0;i<len;i++)
{
hash=(hash<<5+hash)+a[i];
hash%=MOD;
}
if(hash<0) hash+=MOD;
return hash%MOD;
}
void inserthash(char a[],int cnt)
{
int h=gethash(a);
p[cnt].next=hashtable[h];
hashtable[h]=cnt;
}
void searchhash(char *a)
{
int h=gethash(a);
int next=hashtable[h];
while(next!=-1)
{
if(strcmp(p[next].to,a)==0)
{
printf("%s\n",p[next].from);
//cout<<p[next].from<<endl;
return ;
}
next=p[next].next;
}
printf("eh\n");
//cout<<"eh"<<endl;
return ;
}
int main()
{
char tmp[23];
memset(hashtable,-1,sizeof(hashtable));
for(int i=1;;i++)
{
gets(tmp);
if(!strlen(tmp)) break;
sscanf(tmp,"%s %s",p[i].from,p[i].to);
inserthash(p[i].to,i);
}
char t[23];
while(~scanf("%s",t))
{
searchhash(t);
}
return 0;
}