POJ 2503 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”。

解题思路:

本题最好采用字典树,估计用时较少,我采用的是先将所有的字符串按字典序排序,然后对要查找的字符串进行二分查找,结果用时700ms。

代码如下:C/C++代码

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct node
{
    char s1[20],s2[20];
} a[100005];
int cmp(node a,node b)
{
    return strcmp(a.s2,b.s2)<0;
}
int main()
{
    int i,j,low,mid,high,flag=1,len=0;
    char s[50];
    while(gets(s))
    {
        //当是空行时,字符串的长度是零;或者字符串第一位是'\0'
        //if(s[0]=='\0')
        if(strlen(s)==0)
        break;
        //sscanf可以从s中读取两个字符串
        sscanf(s,"%s%s",a[len].s1,a[len].s2);
        len++;
    }
    sort(a,a+len,cmp);//按字典序排列所以字符串
    //二分查找字符串
    while(gets(s))
    {
        low=0;
        high=len-1;
        flag=1;
        while(low<=high)
        {
            int mid =(low+high)>>1;
            if(strcmp(s,a[mid].s2)==0)
            {
                printf("%s\n",a[mid].s1);
                flag = 0;
                break;
            }
            else if(strcmp(s,a[mid].s2)>0)
                low=mid+1;
            else
                high=mid-1;
        }
        if(flag)
            printf("eh\n");
    }
    return 0;
}


字典树代码:

#include<stdio.h>
#include<string.h>
#include<malloc.h>

typedef struct node
{
    char s[20];
    struct node *next[26];
}node;

void insert(char *a,char *b,node *T)
{
    node *p,*q;
    int i,j,id;
    p=T;
    i=0;
    while(b[i])
    {
        id=b[i]-'a';
        if(p->next[id]==NULL)
        {
            q=(node *)malloc(sizeof(node));
            memset(q->s,'\0',sizeof(q->s));
            for(j=0;j<26;++j)
                q->next[j]=NULL;
            p->next[id]=q;
        }
        p=p->next[id];
        ++i;
    }
    strcpy(p->s,a);
}

void search(char *a,node *T)
{
    int id,i=0;
    node *p=T;
    while(a[i])
    {
        id=a[i]-'a';
        if(p->next[id]==NULL)
        {
            printf("eh\n");
            return;
        }
        p=p->next[id];
        ++i;
    }
    if((p->s)[0]!='\0')
        printf("%s\n",p->s);
    else
        printf("eh\n");
}

int main()
{
    char a[20],b[20];
    char c[40];
    node *T;
    int i,len,k;
    T=(node *)malloc(sizeof(node));
    memset(T->s,'\0',sizeof(T->s));
    for(i=0;i<26;++i)
        T->next[i]=NULL;
    while(gets(c)&&c[0]!='\0')  //注意输入格式,输入空行时结束while循环
    {
        len=strlen(c);
        for(i=0;c[i]!=' ';++i)
            a[i]=c[i];
        a[i]='\0';
        for(++i,k=0;i<=len;++i)
            b[k++]=c[i];
        insert(a,b,T);
    }
    while(scanf("%s",a)!=EOF)
    {
        search(a,T);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值