Babelfish
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 35080 | Accepted: 15013 |
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
题解:直接用字符串hash,或者字典树。
代码如下:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<set>
#include<vector>
#include<string.h>
#include<map>
#define inff 0x3fffffff
#define nn 1100000
#define mod 1000007
typedef __int64 LL;
typedef unsigned __int64 LLU;
using namespace std;
char s[110];
bool use[nn];
LLU ha[nn];
string ans[nn];
void add(string x,string y)
{
LLU ix=0;
for(int i=0;i<(int)x.size();i++)
{
ix=ix*131+x[i];
}
int wei=ix%mod;
for(int i=wei;;i=(i+1)%mod)
{
if(!use[i])
{
use[i]=true;
ha[i]=ix;
ans[i]=y;
break;
}
}
}
string Hash(string x)
{
LLU ix=0;
for(int i=0;i<(int)x.size();i++)
{
ix=ix*131+x[i];
}
int wei=ix%mod;
for(int i=wei;;i=(i+1)%mod)
{
if(!use[i])
{
return "eh";
}
else if(ha[i]==ix)
return ans[i];
}
return "eh";
}
int main()
{
int i;
bool ok=true;
memset(use,false,sizeof(use));
while(gets(s))
{
int l=strlen(s);
if(l==0)
{
ok=false;
continue;
}
if(ok)
{
string ix="";
string en,fo;
for(i=0;i<l;i++)
{
if(s[i]==' ')
{
en=ix;
ix="";
}
else
ix+=s[i];
}
fo=ix;
add(fo,en);
}
else
{
string ix=s;
printf("%s\n",Hash(ix).c_str());
}
}
return 0;
}