POJ 2503 Babelfish
来源:互联网 发布:淘宝上网络解锁可靠吗 编辑:程序博客网 时间:2024/06/11 14:31
Babelfish
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 42986 Accepted: 18199
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.
Source
Waterloo local 2001.09.22
//字典树:
#include <iostream>#include <iomanip>#include <cstring>#include <map>#include <cstdio>#include <algorithm>using namespace std;struct Trie{ int cnt; bool flag; char s[45];//保存的字符串 struct Trie *next[26]; Trie():cnt(0),flag(false){memset(next,0,sizeof(next));}};Trie *root = NULL;void Trie_insert(char *str,char *tmp){ Trie *p = root; for(int i=0;i<strlen(str);i++) { if(p -> next[str[i] - 'a' ]== NULL) { p -> next[str[i] - 'a'] = new Trie; } p = p ->next[str[i] - 'a']; } p->cnt++;//走到字符串的最后一个字母的节点strcpy(p->s,tmp);p->flag=1;}void Trie_search(char *str){ Trie *p = root; for(int i=0;i<strlen(str);i++) { if(p -> next[str[i] - 'a'] == NULL) { cout<<"eh"<<endl; return; } else p = p -> next[str[i] - 'a']; }cout<<p ->s<<endl;}int main(){ char str[30],s1[15],s2[15]; root = new Trie; while(gets(str)) { if(str[0] == '\0') break; sscanf(str,"%s %s",s1,s2); Trie_insert(s2,s1); } while(scanf("%s",str)!=EOF) Trie_search(str); return 0;}
//map:
#include <map>#include <cstdio>#include <algorithm>using namespace std;int main(){ char str[30],s1[15],s2[15]; root = new Trie; while(gets(str)) { if(str[0] == '\0') break; sscanf(str,"%s %s",s1,s2); Trie_insert(s2,s1); } while(scanf("%s",str)!=EOF) Trie_search(str); return 0;}int main(){ map<string,string> m; string s1,s2,s; while(getline(cin,s)) { if(s == "") break; s1 = ""; s2 = ""; int sum = 0; for(int i=0;i<s.size();i++) { if(s[i] == ' ') break; s1 += s[i]; ++sum; } sum +=1; for(;sum<s.size();sum++) { s2 += s[sum]; } m[s2] = s1; } while(cin>>s) { if(m[s] != "") cout<<m[s]<<endl; else cout<<"eh"<<endl; } m.clear(); return 0;}
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(二分)
- 总结篇:大型网站技术架构-性能(二)
- 1127. ZigZagging on a Tree (30) PAT 甲级
- JavaScript数据类型(一)
- 单例模式
- --save-dev 与--save的区别
- POJ 2503 Babelfish
- Android布局优化之Include(一)
- Java之多线程下载工具类
- 第一次编译AOSP简单流程
- 表格数据的上移与下移功能
- 计算机文化基础—IT概论
- DFS搜索排列与组合
- sky patch 简单试用
- 终端,进程组,作业,会话及作业控制