poj Babelfish(二分||map)
来源:互联网 发布:sql union 重复 编辑:程序博客网 时间:2024/06/12 00:28
Babelfish
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 ogdaycat atcaypig igpayfroot ootfrayloops oopslayatcayittenkayoopslay
Sample Output
catehloops
Hint
Huge input and output,scanf and printf are recommended.
方法一:map记录并查找
代码:
#include<stdio.h>#include<iostream>#include<map>using namespace std;int main(){ map<string,string>translate; char a[22],b[11],c[11]; while(gets(a)&&a[0]!='\0') { sscanf(a,"%s %s",b,c); translate[c]=b; } while(gets(a)) { if(translate.find(a)!=translate.end()) cout<<translate[a]<<endl; else cout<<"eh"<<endl; } return 0;}
方法二:二分查找
代码:
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;struct node{ char s1[15],s2[15];}a[100010];int len;bool cmp(node a,node b){ return strcmp(a.s2,b.s2)<0;}int main(){ char s[22]; len=0; while(gets(s)&&s[0]!='\0') { sscanf(s,"%s %s",a[len].s1,a[len].s2); len++; } sort(a,a+len,cmp); while(gets(s)) { int x=0,y=len-1,flag=1; while(x<=y) { int mid=(x+y)>>1; if(!strcmp(s,a[mid].s2)) { printf("%s\n",a[mid].s1); flag=0; break; } else if(strcmp(s,a[mid].s2)<0) y=mid-1; else x=mid+1; } if(flag) printf("eh\n"); } return 0;}
1 0
- poj Babelfish(二分||map)
- poj 2503 Babelfish (map / 二分)
- poj2503 Babelfish(map,二分)
- POJ 2503 Babelfish(map)
- POJ 2503 Babelfish(map,字典树,快排+二分,hash)
- POJ 2503 Babelfish(map或hash或排序二分)
- POJ 2503 Babelfish(二分查找)
- poj 2503 Babelfish(二分)
- POJ 2503 Babelfish 二分
- poj 2503 Babelfish (map,trie 树)
- poj 2503 Babelfish(STL map写法)
- POJ-2503 Babelfish(map模板题)
- POJ 2503 Babelfish(人造字典map)
- POJ 2503 Babelfish(hash + map)
- POJ 2503-Babelfish(STL-map)
- POJ 2503 Babelfish ,map
- Babelfish - POJ 2503 map
- POJ 2503-Babelfish(map)
- scrapy定时执行抓取任务
- Hadoop实践(零)---Hadoop作为数据湖
- java数组逆序用for循环,for循环的初始化语句定义了两个变量
- Android-开源框架之XUtils的基本使用
- Java集合系列(10)--TreeSet
- poj Babelfish(二分||map)
- Java Fundamental day03
- 40、Linux初步(CentOS 7下安装配置FTP)
- Hibernate核心API的详解
- Android 官方示例:android-architecture 学习笔记(五)之todo-mvp-clean
- 面试感悟----一名3年工作经验的程序员应该具备的技能
- 如何高效学习
- 【SPOJ 375】Query on a tree&树链剖分详解
- Spring注解@Component、@Repository、@Service、@Controller区别