Babelfish
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
Waterloo local 2001.09.22
输入是难点之一:用sscanf()
gets(ch);
sscanf(ch,”%s%s”,str1,str2);
做了几道trie得出了一条结论,,,当单词的长度超过15时,,适合hash,,,不超过15时适合trie,,,,
因为trie的常数主要乘在了单词长度的循环上,,,,,而hash在这个循环的常数基本是1,,,但是hash此外需要处理冲突,,单词越长,,发成冲突的可能性就越小,,解决冲突的时间就越短,,,,
使用trie有一个隐患,,,,如果用动态内存容易超时,,,用静态内存容易超内存,,,,,
STL_map
//9596K 938MS
#pragma warning(disable:4786)
#include <iostream>
#include <string>
#include <map>
using namespace std;
map <string,string>data;
int main ()
{
#ifdef ONLINE_JUDGE
#else
freopen("read.txt","r",stdin);
#endif
char ch[30],str1[15],str2[15];
while (gets(ch))
{
if (ch[0]==0)
break;
sscanf(ch,"%s%s",str1,str2);
data[str2]=str1;
}
while (gets(ch))
{
map<string,string>::iterator it;
it=data.find(ch);
if (it!=data.end())
cout<<(*it).second<<endl;
else
printf("eh\n");
}
return 0;
}
Trie的静态建树
//15428K 204MS
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
struct Trie
{
char word[12];
int next[30];
}trie[100005*11];
int e;
void Insert (char str[],char ch[])
{
int p=0;
for (int i=0;ch[i];i++)
{
if (trie[p].next[ch[i]-'a'] == 0)
trie[p].next[ch[i]-'a']=++e;
p=trie[p].next[ch[i]-'a'];
}
strcpy(trie[p].word,str);
}
void Output (char str[])
{
int p=0;
for (int i=0;str[i];i++)
{
if (trie[p].next[str[i]-'a']==0)
{
printf("eh\n");
return;
}
p=trie[p].next[str[i]-'a'];
}
printf("%s\n",trie[p].word);
}
int main ()
{
#ifdef ONLINE_JUDGE
#else
freopen("read.txt","r",stdin);
#endif
char ch[30],str1[15],str2[15];
e=1;
while (gets(ch))
{
if (ch[0]==0)
break;
sscanf(ch,"%s%s",str1,str2);
Insert(str1,str2);
}
while (gets(ch))
Output(ch);
return 0;
}
hash
#include <cstdio>
#include <cstring>
const int prime=11681;
struct Node
{
char s1[12],s2[12];
int next;
}node[200005];
char data[200005][25];
int N,head[12000],e;
// BKDR Hash Function
unsigned int hash (char *str)
{
unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
unsigned int key=0;
while (*str)
key = key * seed + (*str++);
return (key%prime+prime)%prime;
// return (hash & 0x7FFFFFFF);
}
/*
int hash (char x[])
{
int len=strlen(x),i,h;
for (h=0,i=0;i<len;i++)
h=h*7+(x[i]-'a')*11;
return (h%prime+prime)%prime; //改为h%prime不会通过,原因:h有可能超过int范围,经过处理一定会变成正值
}*/
void Insert (int key,char s1[],char s2[])
{
strcpy(node[e].s1,s1);
strcpy(node[e].s2,s2);
node[e].next=head[key];
head[key]=e++;
}
bool check (int key,char x[],char ss[])
{
int i;
for (i=head[key];i!=-1;i=node[i].next)
if (strcmp(node[i].s1,x) == 0) //找到
{
strcpy(ss,node[i].s2);
return true;
}
return false;
}
void Deal ()
{
char ss[12],ch[12];
while (~scanf("%s",ch))
{
int tmp=hash(ch);
if (check(tmp,ch,ss))
printf("%s\n",ss);
else
printf("eh\n");
}
}
int main ()
{
char ss[30];
memset(head,-1,sizeof(head));
for (N=1;gets(ss) && strcmp(ss,"")!=0;N+=2)
sscanf(ss,"%s%s",data[N],data[N+1]);
e=0;
for (int i=2;i<=N-1;i+=2)
{
int tmp=hash(data[i]);
Insert(tmp,data[i],data[i-1]);
}
Deal();
return 0;
}