Babelfish
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 35663 | Accepted: 15256 |
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.
字典树!
原来字典树也可以这么玩,原来可不知道!用一个二维数组来存这个字符串的映射值!真的很方便哦1
字典树!
原来字典树也可以这么玩,原来可不知道!用一个二维数组来存这个字符串的映射值!真的很方便哦1
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
char word[100010][15];
struct Tree
{
int num;
Tree *next[26];
Tree()
{
num=0;
for(int i=0;i<26;i++)
{
next[i]=NULL;
}
}
}*root;
void insert(Tree *p,char *s,int n)
{
int i=0;
while(s[i])
{
int x=s[i]-'a';
if(p->next[x]==NULL)
{
p->next[x]=new Tree();
}
p=p->next[x];
i++;
}
p->num=n;
}
int find(Tree *p,char *s)
{
int i=0;
while(s[i])
{
int n=s[i]-'a';
if(p->next[n]==NULL)
{
return 0;
}
else
{
p=p->next[n];
}
i++;
}
return p->num;
}
int main()
{
int n,m,stl,i,j,f=1,ans;
char s[15],c;
char s1[15];
root=new Tree();
while(c=getchar())
{
if(c=='\n')
{
break;
}
else
{
ungetc(c,stdin);
scanf("%s%s%*c",&s,&s1);
insert(root,s1,f);
strcpy(word[f],s);
f++;
}
}
while(gets(s))
{
ans=find(root,s);
if(ans==0)
{
printf("eh\n");
}
else
{
printf("%s\n",word[ans]);
}
}
}