ZOJ 1109 Language of FatMouse

题目地址:点击打开链接

思路:用STL容易超时,本题没有超时,用字典树比较麻烦,可以先排序再二分搜索

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>

using namespace std;

int main()
{
    map<string,string> entry;
    char a[20],b[20],c[40];
    string value,key;
    while(gets(c))
    {
        if(strlen(c) == 0)
            break;
        sscanf(c,"%s%s",a,b);
        value = a;
        key = b;
        entry[key]  = value;
    }
    while(scanf("%s",a) != EOF)
    {
        if(entry.find(a) != entry.end())
            //printf("%s\n",entry[a]);不能用print输出
            cout<<entry[a]<<endl;
        else
            printf("eh\n");
    }
    return 0;
}

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>

using namespace std;

struct node
{
    char english[20],mouse[20];
}word[100105];

int cmp(const void *_a,const void *_b)//按字符串升序排列
{
    node *a = (node*)_a;
    node *b = (node*)_b;
    return strcmp(a->mouse,b->mouse);
}

int main()
{
    char c[40];
    int n = 0;
    while(gets(c))
    {
        if(strlen(c) == 0)
            break;
        sscanf(c,"%s%s",word[n].english,word[n].mouse);
        n++;
    }
    qsort(word,n,sizeof(node),cmp);
    int left,right,mid,ok;
    while(scanf("%s",c) != EOF)
    {
        left = ok = 0;
        right = n - 1;
        while(left <= right)
        {
            mid = (left + right) / 2;
            if(strcmp(word[mid].mouse,c) == 0)
            {
                ok = 1;
                break;
            }
            else if(strcmp(word[mid].mouse,c) > 0)
                right = mid - 1;
            else
                left = mid + 1;
        }
        if(ok)
            printf("%s\n",word[mid].english);
        else
            printf("eh\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值