百炼 2804 词典
来源:互联网 发布:德国女人 下面知乎 编辑:程序博客网 时间:2024/04/30 04:44
2804:词典
总时间限制: 3000ms 内存限制: 65536kB描述你旅游到了一个国外的城市。那里的人们说的外国语言你不能理解。不过幸运的是,你有一本词典可以帮助你。输入首先输入一个词典,词典中包含不超过100000个词条,每个词条占据一行。每一个词条包括一个英文单词和一个外语单词,两个单词之间用一个空格隔开。而且在词典中不会有某个外语单词出现超过两次。词典之后是一个空行,然后给出一个由外语单词组成的文档,文档不超过100000行,而且每行只包括一个外语单词。输入中出现单词只包括小写字母,而且长度不会超过10。输出在输出中,你需要把输入文档翻译成英文,每行输出一个英文单词。如果某个外语单词不在词典中,就把这个单词翻译成“eh”。
样例输入dog ogdaycat atcaypig igpayfroot ootfrayloops oopslayatcayittenkayoopslay样例输出catehloops
比较简单的检索题,主要建立一个词典的结构体,然后建立完毕后用sort对外文进行升序排序,查找时直接进行二分查找,速度很快.值得注意的是构造词典时,词典的输入是以一个回车结束,所以可以使用 getchar() 函数来获取输入的第一个字符,如果是'\n',说明词典的输入停止.然后再输入待查找的外文.
/* 内存: 7688kB 时间: 129ms 语言: G++ */#include <iostream>#include <cstring>#include <string>#include <stdio.h>#include <algorithm>using namespace std;int d_num, flag, res;char temp;char _find[12];struct dictionary{ char Eng[12]; char For[12];}dic[100100];bool cmp(dictionary a,dictionary b){ return strcmp(a.For,b.For)<0;}void dfs(int left,int right,char *des){ int mid = (left+right)/2; if(left <= right) { if(strcmp(dic[mid].For,des) < 0)//在右侧区域 { return dfs(mid+1,right,des); } else if(strcmp(dic[mid].For,des) > 0) { return dfs(left,mid-1,des); } else { flag = 1; res = mid; return; } } return;}int main(){ while((temp=getchar()) != '\n') { dic[d_num].Eng[0] = temp; scanf("%s%s",dic[d_num].Eng+1,dic[d_num].For); d_num++; getchar();//行尾回车 } sort(dic,dic+d_num,cmp);//词典按外文排序 while(scanf("%s",&_find) != EOF) { flag = 0; dfs(0,d_num-1,_find); if(flag == 0) printf("eh\n"); else { printf("%s\n",dic[res].Eng); } } return 0;}
1 0
- 百炼 2804 词典
- OpenJudge百炼-2804-词典-C语言-二分查找
- 百炼
- 百炼采药问题....
- 百炼 1833:排列
- 百炼-2726:采药
- poj百炼 2788
- 【LIS】百炼-2757
- 百炼OJ1001
- 百炼OJ1002
- 百炼OJ1003
- 百炼OJ1004
- 百炼OJ1005
- 百炼OJ1006
- 百炼OJ1007
- 百炼OJ1009
- 百炼 Zipper 剪枝
- 百炼2787
- 微信小程序开发-短信注册功能
- 第二章 CDN技术概述
- PHP高级——抽象类与接口的区别
- listview取消按下效果
- sublime3 BufferScroll插件实现折叠功能
- 百炼 2804 词典
- 简单的SQL常识(待续)
- UML—用例图
- White space is required before the attribute type in the declaration of attribute "t" for element "b
- Collections类常用方法总结
- Jquery的筛选
- 自定义VIEW(学习笔记一)-基础知识
- Eclipse && Android Studio 连接手机的问题及解决方案
- 1