2503:Babelfish(二分查找)
2503:Babelfish
- 总时间限制:
- 3000ms
- 内存限制:
- 65536kB
- 描述
- 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".
- 样例输入
-
dog ogday cat atcay pig igpay froot ootfray loops oopslay atcay ittenkay oopslay
- 样例输出
-
cat eh loops
- 提示
- Huge input and output,scanf and printf are recommended.
- 来源
- Waterloo local 2001.09.22
-
#include <bits/stdc++.h> using namespace std; struct Entry{ string a,b; }zd[100000]; bool cmp(Entry a,Entry b){ //按照字母升序排序 return a.a<b.a; } int main(){ string s1,s2,s; int num=0; while(cin>>s1>>s2){ zd[num].a=s2;zd[num].b=s1; num++; cin.get(); //去掉回车符 if(cin.peek()=='\n')break; //若读入的是空行 } sort(zd,zd+num,cmp); while(cin>>s){ int left=0,right=num; while(left<=right){ int mid=left+(right-left)/2; if(s==zd[mid].a){ cout<<zd[mid].b<<endl; break; } else if(s<zd[mid].a)right=mid-1; else left=mid+1; } if(left>right)cout<<"eh"<<endl; } return 0; }
越努力越幸运