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

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值