您的位置:首页 > 其它

文章标题 POJ 2503 : Babelfish(字典树)

2016-11-13 21:37 543 查看

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.

题意:你去到了一个说外语并且听不懂的地方,但还好你有笔记,现给你一个单词,能否在笔记中找到相对应的英文单词,能的话就输出英文单词,不能的话就呼出eh

分析:用字典树将外语记录下来,当到达最后一个字母时,将对应的英文单词记录下来。

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<math.h>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
const int inf = 0x3f3f3f3f;
struct node {
char eng[15];//记录英文单词用的
node *nex[26];
};
node *root;
node* create(){//创建一个节点,将其nex赋我NULL
node *p=new node ;
for (int i=0;i<26;i++){
p->nex[i]=NULL;
}
return p;
}
void insert(char* foreign,char* eng){//插入单词,并将对应的英文单词保存下来
node *p=root;
int k;
for (int i=0;foreign[i];i++){
k=foreign[i]-'a';
if (p->nex[k]==NULL){
p->nex[k]=create();
}
p=p->nex[k];
}
strcpy(p->eng,eng);//保存
}
int search(char* foreign,char* eng){//查找,如果存在将对应的英文单词保存在eng中
node *p=root;
int k;
for (int i=0;foreign[i];i++){
k=foreign[i]-'a';
if (p->nex[k]==NULL) return 0;
p=p->nex[k];
}
strcpy(eng,p->eng);//保存
return 1;
}
int main ()
{
char eng[15],foreign[15];
root = create();
while (1){
char t;
if ((t=getchar())=='\n') break;//判断是否输入结束
eng[0]=t;
int i=1;
while (1){
t=getchar();
if (t==' '){//判断是否是该单词输入结束
eng[i]='\0';
break;
}
eng[i++]=t;
}
scanf ("%s",foreign);
getchar();//吃掉换行
insert(foreign,eng);
}
char word[15];
while(scanf ("%s",word)!=EOF){
char english[15];
int flag=search(word,english);
if (flag){
printf ("%s\n",english);
}
else printf ("eh\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj