POJ - 2503 Babelfish
来源:互联网 发布:linux usr local bin 编辑:程序博客网 时间:2024/06/10 17:27
Babelfish
Time Limit: 3000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u
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.
#include<iostream> //该题用暴力求解会出现Time Limit Exceeded#include<cstring> //二分查找是对有序数组的查找 可以优化效率 查找时间为O(lgn)#include<string>#include<cstdio>#include<algorithm>using namespace std;const int MAXN = 100100;struct dii //用结构体定义字典{char d[25];char di[25];}p[MAXN];bool cmp(dii a, dii b) //定义对结构体的比较{ return strcmp(a.di, b.di)<0;}int find(int sl, int sr, char target[25]) {int l = sl, r = sr, mid; //核心:二分法查找while (l <= r){mid = (l + r) >> 1;if (strcmp(p[mid].di, target) == 0) return mid; //找到则返回下标else if (strcmp(p[mid].di, target)>0) r = mid - 1; //目标小于中点,改变区间右端在【a,(a+b)/2-1】查找else if (strcmp(p[mid].di, target)<0) l = mid + 1; //目标大于中点,改变区间左端在【(a+b)/2+1,b】查找}return -1; //找不到返回-1;}int main(){char s[25];int i = 0;while (gets(s)){if (s[0] == '\0') break;sscanf(s, "%s%s", p[i].d, p[i].di); //sscanf函数用于读入整行数据i++;}sort(p, p + i, cmp); //二分查找是在顺序基础上进行的char c[25];while (cin >> c){int ans = find(0, i - 1, c);if (ans == -1) cout << "eh" << endl;else cout << p[ans].d << endl;}}
0 0
- poj 2503 Babelfish
- POJ 2503 Babelfish(HASH)
- poj 2503 Babelfish
- POJ 2503 Babelfish
- POJ 2503 Babelfish
- poj 2503 Babelfish
- POJ 2503 Babelfish
- poj 2503 Babelfish 【hash】
- poj 2503 Babelfish【trie】
- POJ 2503 Babelfish
- poj-2503 Babelfish
- poj-2503 Babelfish
- poj 2503 Babelfish
- poj 2503 Babelfish
- POJ 2503 Babelfish
- POJ 2503 Babelfish (Hash)
- POJ 2503 Babelfish
- poj 2503 Babelfish(二分)
- AndroidStudio 笔记
- codeforces 213C Relay Race(DP基础,棋盘路径)
- Xargs用法详解
- C++ Exception
- Problem 165D - Beard Graph 树链剖分
- POJ - 2503 Babelfish
- 软件开发之路的十字路口
- 编码规范
- LeetCode - Maximal Rectangle
- 重新写了一个简单的日志类
- 远程桌面连接终极解决——常见问题及解决办法(亲测)
- 黑马程序员——OC—block和protocol协议
- codeforces 118D Caesar's Legions(DP进阶)
- POJ - 2299 Ultra-QuickSort