Language of FatMouse

Language of FatMouse

时间: 1ms        内存:64M

描述:

We all know that FatMouse doesn't speak English. But now he has to be prepared since our nation will join WTO soon. Thanks to Turing we have computers to help him.

输入:

Input consists of up to 100,005 dictionary entries, followed by a blank line, followed by a message of up to 100,005 words. Each dictionary entry is a line containing an English word, followed by a space and a FatMouse word. No FatMouse word appears more than once in the dictionary. The message is a sequence of words in the language of FatMouse, 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. FatMouse 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

提示:

参考答案(内存最优[1492]):

#include <iostream>
#include <cstring>
#include <cstdio>
#include <map>
using namespace std;
string s,aa,bb;
char str[100];
char a[100],b[100];
int main()
{
    int i;
    map<string,string> ma;
    while(gets(str))
    {
        int len=strlen(str);
        if(len==0)break;
        sscanf(str,"%s%s",a,b);
        aa=a;
        bb=b;
        ma[bb]=aa;
    }
    while(cin>>s)
    {
        if(!ma.count(s))cout<<"eh"<<endl;
        else
        cout<<ma[s]<<endl;
    }
    return 0;
}

参考答案(时间最优[0]):

#include<stdio.h>
#include<string.h>
struct
{
    char z[15],j[15];
} c[100010];
int fac(int n, char *a)
{
    int left=0,right=n,mid;
    while(left<right)
    {
        mid=(left+right)/2;
        if(strcmp(c[mid].j,a) == 0)
            return mid;
        else if(strcmp(c[mid].j,a) >  0)
            right=mid;
        else
            left = mid + 1;
    }
    return -1;
}
int main()
{
    int n=0,k=1;
    char a[30];
    while(gets(a)&&strlen(a)!=0)
    {
        sscanf(a,"%s%s",c[n].z,c[n].j);
        n++;
    }
    while(gets(a))
    {
        k=fac(n,a);
        if(k==-1)
            puts("eh");
        else
            puts(c[k].z);
    }
    return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注