您的位置:首页 > 其它

Babelfish(二分)

2015-09-24 21:11 267 查看
Babelfish

Time Limit: 3000MSMemory Limit: 65536K
Total Submissions: 37238Accepted: 15879
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
题解:此处用到了sscanf的用法,很巧妙;
代码:


#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int MAXN=100010;
struct Node{
char en[20],ei[20];//无语开小了。。。
};
Node dt[MAXN];
int cmp(Node a,Node b){
return strcmp(a.ei,b.ei)<0;
}
int erfen(char *x,int l,int r){
while(l<=r){
int mid=(l+r)>>1;
if(!strcmp(dt[mid].ei,x))return mid;
if(strcmp(dt[mid].ei,x)>0)r=mid-1;
else l=mid+1;
}
return -1;
}
int main(){
int dm=0;
char str[50];
Node a;
while(gets(str)){
if(str[0]=='\0')break;
sscanf(str,"%s%s",a.en,a.ei);//这里很巧妙。。。
dt[dm++]=a;
}
sort(dt,dt+dm,cmp);
while(~scanf("%s",str)){
int t=erfen(str,0,dm-1);
if(t==-1)puts("eh");
else printf("%s\n",dt[t].en);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: