Babelfish
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 26214 | Accepted: 11249 |
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
考察点:字典树
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
struct Tire
{
struct Tire* next[26];
int tag;
};
char s1[100010][15],s2[15];
int main()
{
struct Tire *newnode();
void build(char s[15],int k,struct Tire *p);
int find(char s[15],struct Tire *p);
struct Tire *head;
int i,j,n,m,s,t,k;
t=1;
head=newnode();
while(1)
{
gets(s2);
if(strlen(s2)==0)
{
break;
}
for(i=0,j=0;i<=strlen(s2)-1;i++)
{
if(s2[i]!=' ')
{
s1[t][j++]=s2[i];
}else
{
break;
}
}
s1[t][j]='\0';
for(i=i+1,j=0;i<=strlen(s2)-1;i++)
{
s2[j]=s2[i]; j++;
}
s2[j]='\0';
build(s2,t,head);
t++;
}
while(scanf("%s",s2)!=EOF)
{
k=find(s2,head);
if(k==0)
{
printf("eh\n");
}else
{
printf("%s\n",s1[k]);
}
}
return 0;
}
struct Tire* newnode()
{
struct Tire *p;
p=new(struct Tire);
for(int i=0;i<=25;i++)
{
p->next[i]=NULL;
}
p->tag=0;
return p;
}
void build(char s[15],int k,struct Tire *p)
{
int a;
for(int i=0;i<=strlen(s)-1;i++)
{
a=s[i]-'a';
if(p->next[a]==NULL)
{
p->next[a]=newnode();
}
p=p->next[a];
}
p->tag=k;
}
int find(char s[15],struct Tire *p)
{
int a;
for(int i=0;i<=strlen(s)-1;i++)
{
a=s[i]-'a';
if(p->next[a]!=NULL)
{
p=p->next[a];
}else
{
return 0;
}
}
return p->tag;
}