[二分查找]Babelfish uva 10282
来源:互联网 发布:淘宝宝贝怎么发布 编辑:程序博客网 时间:2024/05/18 03:16
Problem C: Babelfish
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".
Sample Input
dog ogdaycat atcaypig igpayfroot ootfrayloops oopslayatcayittenkayoopslay
Output for Sample Input
catehloops
这道题题意非常简单,给出一个字典,找出对应单词的翻译。这种类型直接用map非常简单,可是我非常手贱的写了一个二分查找,其中还出现一些小错误。
#include<iostream>#include<algorithm>#include<string>#include<cstdio>using namespace std;int k;class trans{public: string st1,st2;}word[100005];bool cmp(trans x,trans y){ return x.st2<y.st2;}int search(string str){ int left=0,right=k-1,mid; while(left<=right) { mid=(left+right)/2; if(word[mid].st2<str) { left=mid+1; } else if(word[mid].st2>str) { right=mid-1; } else return mid; } return -1;}int main(){ string str; k=0; while(cin>>str) { char ch; ch=getchar(); if(ch!=' ') { sort(word,word+k,cmp); int r=search(str); if(r!=-1) { cout<<word[r].st1<<endl; } else { cout<<"eh"<<endl; } break; } word[k].st1=str; cin>>str; word[k].st2=str; k++; } while(cin>>str) { int cnt=search(str); if(cnt!=-1) { cout<<word[cnt].st1<<endl; } else { cout<<"eh"<<endl; } } return 0;}
1 0
- [二分查找]Babelfish uva 10282
- UVa 10282 - Babelfish
- uva 10282 - Babelfish
- uva 10282 - Babelfish
- uva-10282 - Babelfish
- uva 10282 - Babelfish
- UVA 10282 Babelfish
- UVa 10282 - Babelfish
- UVA 10282 - Babelfish
- UVA 10282 Babelfish
- uva 10282 Babelfish
- UVA 10282 - Babelfish
- UVa 10282 - Babelfish
- UVA - 10282 Babelfish
- uva 10282 Babelfish
- uva 10282 Babelfish
- UVA - 10282 Babelfish
- poj2503-Babelfish(快速排序+二分查找)
- .Net在64位系统下连接Access数据库的问题The 'Microsoft.Jet.OLEDB.4.0' provider is not registered on the local machi
- Tiny6410 移植tslib1.4
- 全世界国家下拉框(附带SQL执行脚本)
- Eclipse配置Struts2问题:ClassNotFoundException: org...dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
- Nodejs Native AddOn的编写
- [二分查找]Babelfish uva 10282
- php 简单生成 excel文件
- HDOJ 2196 Computer
- Android多线程方式处理图片下载及显示
- mac下安装wordpress以及配置相关环境
- 一种中文文本的快速分词方法(二)
- codeforces Strings of Power
- ZOJ 3201 Tree of Tree
- Struts2模型驱动