Babelfish

题目描述

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 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 is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as "eh". 

 

样例输入

dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay

 

样例输出

cat
eh
loops

 

提示

Huge input and output,scanf and printf are recommended.

这道题,可以用map做,map里的查询可以调用自带函数,也可以自己写结构体,用二分实现map;然而,我写了几次,一直存在bug,没办法,套用字典树模板ac过的。字典树,时间上应该更优。

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mpxx=100050;
char s[mpxx][20],c[20];
struct node
{
    int flag;
    node *next[26];
}*head;
int top;
node *creat()//初始化字典树
{
    node *p;
    p=new node;
    p->flag=0;
    for(int i=0;i<26;i++)
    {
        p->next[i]=NULL;
    }
    return p;
}
void build()//字典树的构建
{
    node *p=head;//p是不断更新的节点
    int a,len=strlen(c);
    for(int i=0;i<len;i++)
    {
        a=c[i]-'a';
        if(!p->next[a])
        {
            p->next[a]=creat();
        }
        p=p->next[a];
    }
    p->flag=top;
}
int query()//查询操作
{
    node *p=head;
    int a,len=strlen(c);
    for(int i=0;i<len;i++)
    {
        a=c[i]-'a';
        if(!p->next[a])
        {
            return 0;
        }
        p=p->next[a];
    }
    if(!p->flag)
    {
        return 0;
    }
    else
    {
        return p->flag;
    }
}
int main()
{
    head=creat();
    top=1;
    int t;
    char cc[40];
    while(gets(cc)&&cc[0]!='\0')
    {
        sscanf(cc,"%s %s",s[top],c);
        build();
        top++;
    }
    while(scanf("%s",c)!=EOF)
    {
        t=query();
        if(t)
        {
            printf("%s\n",s[t]);
        }
        else
        {
            printf("eh\n");
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值