POJ 2503 Babelfish

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值