POJ 2503 Babelfish (字典树)
2013-04-28 16:44
375 查看
Babelfish
Time Limit : 6000/3000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)Total Submission(s) : 53 Accepted Submission(s) : 13
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
Output is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as "eh".
[align=left]Sample Input[/align]
dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay
atcay
ittenkay
oopslay
Sample Output
cat
eh
loops
题目大意:在字典中的存入 英语 和对应方言, 然后查找输入的方言 ,如果字典中存在,输出对应的英语;否则输出“eh”
思路有两个:一种是将字典排序后二分查找,另一种是用字典树(前缀树)Trie。第一次写了Trie的代码,参考的《入门经典》,纪念一下
# include<stdio.h> # include<stdlib.h> # include<string.h> # define maxnode 200005 char ans[maxnode][30]; //字典树中结点(方言)对应的英语 //构造字典树 struct Trie{ int ch[maxnode][26]; int val[maxnode]; int sz; //结点总数 Trie() {sz=1; memset(ch[0],0,sizeof(ch[0])); } //初始化时只有一个根结点 int idx(char c) { return c-'a';} void insert(char *s,int v){ //插入字符串 //插入字符串是,附加信息为v。注意v必须为非0,因为0代表“本结点不是单词结点” int u=0,n=strlen(s); for(int i=0;i<n;i++){ int c=idx(s[i]); if(!ch[u][c]){ //结点不存在 memset(ch[sz],0,sizeof(ch[sz])); val[sz]=0; //中间结点的附加信息为0 ch[u][c] = sz++; //新建结点 } u=ch[u][c]; //往下走 } val[u] = v; //字符串最后一个字符的附加信息为v } int find(char *s){ //查找字符串 int u=0,n=strlen(s); for(int i=0;i<n;i++){ int c=idx(s[i]); if(!ch[u][c]){ return 0; } u=ch[u][c]; } return val[u]; } }s; int main(){ int num=1,i,j; char ss[30]; //要查找字符串 char s1[30],s2[30],s3[60]; //s1英语,s2方言,s3为了检查字典写完而存在的 //输入字典 while(1){ gets(s3); int len = strlen(s3); if(len==0) break; int flag=0; for(i=0,j=0;i<len;i++){ if(s3[i]==' ') {s1[j]=0; flag=1; j=0; continue;} if(flag==0) s1[j++]= s3[i]; else s2[j++] = s3[i]; } s2[j]=0; strcpy(ans[num],s1); s.insert(s2,num); num++; } //查找单词 while(scanf("%s",ss)!=EOF){ if(s.find(ss)==0) //没找到 printf("eh\n"); else { printf("%s\n",ans[s.find(ss)]); } } return 0; }
View Code
相关文章推荐
- POJ - 2503 - Babelfish(字典树)
- poj 2503 Babelfish(字典树或map或哈希或排序二分)
- POJ_2503_Babelfish【字典树】
- poj 2503 Babelfish(字典树)
- POJ 2503 Babelfish 字典树
- POJ 2503-Babelfish【字典树】
- POJ 2503 Babelfish 字典树应用
- POJ 2503 Babelfish(字典树)
- poj 2503 Babelfish(字典树或着STL)
- poj 2503 Babelfish(字典树或STL水题) 解题报告(百炼2804)
- POJ 2503:Babelfish(字典树入门)
- POJ_2503_Babelfish(map or 字典树)
- POJ 2503 Babelfish(字典树Trie)
- POJ2503 - Babelfish - 字典树
- poj-2503-Babelfish-字典树
- poj 2503 Babelfish(字典树)
- POJ 2503 Babelfish 字典树经典题 三种方法 (map,排序+二分,字典树)
- poj 2503 Babelfish 字典树
- POJ 2503 Babelfish map水过,字典树????
- POJ 2503 Babelfish(字典树Trie)