Babelfish(二分查找,字符串的处理略有难度,用sscanf输入)

Babelfish
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 28581   Accepted: 12326

题目链接:http://poj.org/problem?id=2503

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.

Source

代码:
 #include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
using namespace std;
struct vode
{
char eng[];
char dic[];
};
struct vode g[];
int strr ( const void *a , const void *b )
{
return strcmp( ((vode *)a)->dic , ((vode *)b)->dic );
}
int binsearch(int s,int t,char dic[])
{
int low=s,high=t,mid;
if(s<=t)
{
mid=low+(high-low)/;
if(strcmp(g[mid].dic,dic)==)return mid;
if(strcmp(g[mid].dic,dic)>)
return binsearch(low,mid-,dic);
else
return binsearch(mid+,high,dic);
}
return -;
}
int main()
{
char dic[];
int i;
//下面的for循环控制字典的输入和结束,是本题的重点所在******
for(i=;dic[]!='\0';i++)
{
gets(dic);
sscanf(dic,"%s%s",g[i].eng,g[i].dic);
//printf("dic[%d]:%s g[%d].eng:%s g[%d].dic:%s\n\n",i,dic,i,g[i].eng,i,g[i].dic);//验证输出
}
qsort(g,i,sizeof(g[]),strr);
while(scanf("%s",dic)!=EOF)
{
int flag=binsearch(,i-,dic);
if(flag>=)cout<<g[flag].eng<<endl;
else cout<<"eh"<<endl;
}
return ;
}
上一篇:VS2010中使用命令行参数 分类: c/c++ 2014-07-11 22:24 634人阅读 评论(0) 收藏


下一篇:leetcode 15. 3Sum 双指针