ZCMU-1787-Babelfish

来源:互联网 发布:阿里云部署git服务器 编辑:程序博客网 时间:2024/05/16 05:03

1787: Problem C: Babelfish

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 53  Solved: 13
[Submit][Status][Web Board]

Description

Problem C: 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".

Input

Output

Sample Input

dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay

Sample Output

cat
eh
loops


【解析】
这道题附上三种解法,前两种是超时的不过我觉得都是有借鉴之处的。最后一种二分查找效率比较高所以自然通过
了。第一种就是map标记通过对map键值得查找来进行。第二种也差不多只不过这个时候我们反着来,让前面的成为
键值,后面的成为下标然后再进行判断就好了,这里需要注意的是空行我们要怎么做可以让我们输入的前半部分结
束紧接着是查询,这里用到了一个gets(s)我们在输入换行的时候判断一下s的长度是不是0就好了。还有需要注意的
就是strtok的用法,是这样的如果以空格符为分界线比如说有这么个字符串abc cbb cbb我们要把这个字符串分隔成
三个小的字符串我们就需要这么做s1=strtok(s," ");s2=strtok(NULL," "); s3=strtok(NULL," ");这样就可以了注
意的是我们在分隔第二个的时候strtok的第一个参数要是NULL以及后面的也是如此。二分查找的。关于二分查找的这
个也是自行百度了一下所得到的发现确实如此..长知识了。sscanf的用法有一种也是以空格符为分隔的用法这个大家
可以去了解一下。我发现二分查找真的是相当有用啊很多都能用的到。

map
#include<iostream>#include<string>#include<map>#include<cstring>#include<cstdio>using namespace std;map<string,string>a;int main(){    string s1,s2,s3;    char c[1001];    while(1)    {        gets(c);        if(strlen(c)==0)            break;         s1=strtok(c," ");         s2=strtok(NULL," ");          a[s1]=s2;    }    map<string,string>::iterator it;     while(cin>>s3)     {         int flag=0;         for(it=a.begin();it!=a.end();it++)         {             if(it->second==s3)             {  flag=1;                cout<<it->first<<endl;                break;             }         }         if(flag==0)         {             cout<<"eh"<<endl;         }     }     return 0;}
#include<string>#include<map>#include<iostream>#include<cstring>#include<cstdio>using namespace std;map<string,string>a;int main(){    string s1,s2,s3,s4;    char c[1001];    while(1)    {        gets(c);        if(strlen(c)==0)            break;         s1=strtok(c," ");         s2=strtok(NULL," ");          a[s2]=s1;//让前面的成为键值,后面的成为下标    }    map<string,string>::iterator it;     while(cin>>s3)     {           s4=a[s3];           if(s4=="")            printf("eh\n");           else            cout<<s4<<endl;     }     return 0;}//map逆着用

二分查找
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>using namespace std;struct node{    char s1[20],s2[20];} a[100005];int len;int cmp(node a,node b){    return strcmp(a.s2,b.s2)<0;//根据后面的字符串的大小进行排序}int main(){    len = 0;    int i,j;    char str[50];    while(gets(str))    {        if(str[0] == '\0')        break;        sscanf(str,"%s%s",a[len].s1,a[len].s2);//以空格作为分割符,sscanf如果有返回值的话就是这里面参数的个数        len++;    }    sort(a,a+len,cmp);    while(gets(str))    {        int l = 0,r= len-1,mid,flag = 1;        while(l<=r)        {            int mid = (l+r)/2;            if(strcmp(str,a[mid].s2)==0)            {                printf("%s\n",a[mid].s1);                flag = 0;                break;            }            else if(strcmp(str,a[mid].s2)<0)                r = mid-1;//表示我们str比a[mid].s2小,所以我们往前面找            else                l = mid+1;//我们应该往后面找        }        if(flag)            printf("eh\n");    }    return 0;}
strtok的基本用法
#include<iostream>#include<string>#include<cstdio>#include<cstring>using namespace std;int main(){    string s1,s2,s3;    char s[101];    gets(s);    s1=strtok(s," ");    s2=strtok(NULL," ");    s3=strtok(NULL," ");    cout<<s1<<endl;    cout<<s2<<endl;    cout<<s3<<endl;    return 0;}




0 0
原创粉丝点击