您的位置:首页 > 其它

POJ-2503-Babelfish

2016-08-08 09:51 357 查看
Babelfish

Time Limit: 3000MSMemory Limit: 65536K
Total Submissions: 40265Accepted: 17159
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.

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
struct node
{
char s1[11];
char s2[11];
}xt[100001];
int BinSearch(int low,int high,char key[])// 二分查找
{
if (low<=high)
{
int mid = (low+high)/2;
if(strcmp(xt[mid].s2, key)==0)
return mid;
else if(strcmp(xt[mid].s2, key)>0)
return BinSearch(low,mid-1,key);
else if(strcmp(xt[mid].s2, key)<0)
return BinSearch(mid+1,high,key);
}
return -1;
}
bool cmp(node a, node b)
{
if(strcmp(a.s2, b.s2)<0)return 1;
return 0;
}
int main()
{
char s[30], s1[15], s2[15];
int top = 0, i, j, flag = 0, sum;
while(1)
{
gets(s);
if((int)strlen(s)==0)break;
flag = 0;
sum = 0;
for(i = 0;i<(int)strlen(s); ++i)
{
if(s[i]!=' '&&flag==0)
{
s1[i] = s[i];
}
else if(s[i]==' ')
{
s1[i] = '\0';
flag=i;
j = 0;
}
else if(s[i]!=' '&&flag)
{
s2[j++] = s[i];
sum+=(s[i]-'a'+1);
}
}
s2[j] = '\0';
strcpy(xt[top].s1, s1);
strcpy(xt[top].s2, s2);
top++;
}
sort(xt, xt+top, cmp);
while(gets(s)!=NULL)
{
int kl = BinSearch(0,top-1,s);
if(kl!=-1)
{
printf("%s\n", xt[kl].s1);
}
else printf("eh\n");
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: