B - 02

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.


题意:

给你几组字符串,前面是单词,后面是索引词,要求输入索引词便能输出单词!!!

思路:

首先考虑的是运用STL方面的知识来解决,但是你会发现TLE!又因为这是查找问题,所以使用二分查找!首先对单词进行排序,然后找到中间点,一层层往下推!

细节:

注意sscanf和printf的使用,可以提高程序效率!还要自己定义比较函数,

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#include <map>
using namespace std;
struct word {
    char from[20],to[20];
} dic[100010];
bool cmp(const word &a,const word &b) {
    if(strcmp(a.from,b.from) <= 0)
        return true;
    return false;
}
int bsearch(int lt,int rt,char *str) {
    int mid;
    while(lt <= rt) {
        mid = (lt+rt)>>1;
        if(strcmp(dic[mid].from,str) == 0) return mid;
        if(strcmp(str,dic[mid].from) < 0) rt = mid-1;
        else lt = mid+1;
    }
    return -1;
}
int main() {
    int cnt = 0,i,ans;
    char str[50];
    while(gets(str) && str[0] != '\0') {
        sscanf(str,"%s %s",dic[cnt].to,dic[cnt].from);
        cnt++;
    }
    sort(dic,dic+cnt,cmp);
    while(gets(str)) {
        ans = bsearch(0,cnt,str);
        if(ans == -1) puts("eh");
        else printf("%s\n",dic[ans].to);
    }
    return 0;
}
心得:

一般查找问题,优先使用二分查找!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值