POJ_2503_Babelfish(map or 字典树)
来源:互联网 发布:javascript调用数据库 编辑:程序博客网 时间:2024/06/05 23:56
Babelfish
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 34816 Accepted: 14908
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
题意:就是对应翻译,如果没有找到翻译就输出“eh”。
分析:首选就想到了map去做,然后AC了。最近在做字典树的题目,于是用字典树也写了一遍。
题目链接:http://poj.org/problem?id=2503
map代码:
trie代码:
题意:就是对应翻译,如果没有找到翻译就输出“eh”。
分析:首选就想到了map去做,然后AC了。最近在做字典树的题目,于是用字典树也写了一遍。
题目链接:http://poj.org/problem?id=2503
map代码:
#include<map>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int MAX = 27 ;const int MAXN = 100000 + 5 ;map<string,int>m;int num = 0 , len;char s[MAXN][MAX],str[MAXN];int main(){ //freopen("liuchu.txt","r",stdin); while(gets(s[num])&&s[num][0]!='\0'){ len = strlen(s[num]); int first = 0, k = 0; for(int i=0;i<len;i++){ if(first) str[k++]=s[num][i]; else if(s[num][i]==' '){ first = 1; s[num][i]='\0'; } } m[str]=num++; memset(str,'\0',sizeof(str)); } while(scanf("%s",str)!=EOF){ if(m.count(str)) printf("%s\n",s[m[str]]); else printf("eh\n"); } return 0;}
trie代码:
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;typedef long long ll;const int MAX = 27 ;const int MAXN = 100000 + 5 ;struct trie{ int point; trie *next[MAX];};int number = 0 ;char s[MAXN][MAX],str[MAX];trie *root=new trie();void createTrie(char *s,int n){ trie *p=root,*q; int len=strlen(s),pos; for(int i=0;i<len;i++){ pos=s[i]-'a'; if(p->next[pos]==NULL){ q=new trie(); for(int j=0;j<MAX;j++) q->next[j]=NULL; p->next[pos]=q; p=p->next[pos]; } else{ p=p->next[pos]; } } p->point=n;}int findTrie(char *s){ trie *p=root; int len=strlen(s),pos; for(int i=0;i<len;i++){ pos=s[i]-'a'; if(p->next[pos]==NULL) return -1; p=p->next[pos]; } return p->point;}void delTrie(trie *Root){ for(int i=0;i<MAX;i++){ if(Root->next[i]!=NULL) delTrie(Root->next[i]); } free(Root);}int main(){ //freopen("liuchu.txt","r",stdin); for(int i=0;i<MAX;i++) root->next[i]=NULL; while(gets(s[number])&&s[number][0]!='\0'){ int len=strlen(s[number]),k=0 ; bool judge=false; for(int i=0;i<len;i++){ if(judge) str[k++]=s[number][i]; if(s[number][i]==' '){ s[number][i]='\0'; judge=true; } } createTrie(str,number); number++; } while(scanf("%s",str)!=EOF){ int pose=findTrie(str); if(pose==-1) printf("eh\n"); else printf("%s\n",s[pose]); } delTrie(root); return 0;}
0 0
- POJ_2503_Babelfish(map or 字典树)
- poj_2503_Babelfish(字典树&&map)
- POJ_2503_Babelfish【字典树】
- Babelfish(字典树 or map)
- HDOJ 1251 统计难题(map or 字典树)
- 第四届“图灵杯”NEUQ-ACM程序设计竞赛(团队赛)-网络同步赛J(字典树 or map)
- Phone List(字典树 or 排序)
- Babelfish(字典树-map映射功能)
- HDU 1004 (字典树||map)
- hdu1251(map用法/字典树模板)
- poj2643(map+字典树)
- 字典树+map+hdu1800
- POJ2503Babelfish(MAP+字典树)
- hdu1247(字典树/map)
- poj 2503 Babelfish(hash or 字典树)
- HDU 5536 Chip Factory(暴力 or 01字典树)
- 字典树实现map容器
- hnuoj 字典树 map c++
- 你真的懂iOS的autorelease吗?
- 使用TDI与WinSock进行客户端服务器编程
- java基础概念之多态
- (9)设计模式:State
- Sicily 13910. Opening Ceremony
- POJ_2503_Babelfish(map or 字典树)
- JVM中一些容易混淆的概念
- Leetcode: Find Peak Element
- greenplum4.3的安装与配置
- Mysql主从配置,实现读写分离
- cookie,request,session与application页面之间参数的传递
- windows中用Virtual Box虚拟机安装ubuntu的一些问题
- iOS:一种直接修改frame的某个属性的方法
- srm 304 div2 1000(环形DP进阶,几何)