您的位置:首页 > 其它

POJ 2503 Babelfish[字典树]

2012-04-10 18:52 387 查看
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

View Code

#include<stdio.h>
#include<string.h>
struct node
{
int count;
struct node*next[26];
node()
{
count=-1;
memset(next,0,sizeof(next));
}
};
char w[100003][33];
void add(char *a,node* root,int num)
{
int x,i=0;
node*p=root;
while(a[i])
{
x=a[i]-'a';
if(p->next[x]==NULL)
p->next[x]=new node();
p=p->next[x];
i++;
}
p->count=num;
}
int find(char *a,node*root)
{
int x,i=0;
node *p=root;
while(a[i])
{
x=a[i]-'a';
if(p->next[x]==NULL)
return -1;
p=p->next[x];
i++;
}
return p->count;
}
int main()
{
char s[33];
int i=0;
node* root;
root=new node();
while(gets(s)&&s[0]!=0)
{
sscanf(s,"%s %s",w[++i],s);
add(s,root,i);
}
while(scanf("%s",s)!=EOF)
{
int k=find(s,root);
if(k!=-1)
printf("%s\n",w[k]);
else printf("eh\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: