POJ 2503 Babelfish (字典树)

Babelfish

Time Limit : 6000/3000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 53   Accepted Submission(s) : 13
Problem 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 ogday
cat atcay
pig igpay
froot ootfray
loops oopslay
 
atcay
ittenkay
oopslay
  Sample Output
cat
eh
loops
 
题目大意:在字典中的存入 英语 和对应方言, 然后查找输入的方言 ,如果字典中存在,输出对应的英语;否则输出“eh”
思路有两个:一种是将字典排序后二分查找,另一种是用字典树(前缀树)Trie。第一次写了Trie的代码,参考的《入门经典》,纪念一下
 
 1 # include<stdio.h>
 2  # include<stdlib.h>
 3  # include<string.h>
 4  # define maxnode 200005
 5  char ans[maxnode][30];    //字典树中结点(方言)对应的英语
 6  //构造字典树
 7  struct Trie{
 8      int ch[maxnode][26];
 9      int val[maxnode];
10      int sz;        //结点总数
11      Trie() {sz=1; memset(ch[0],0,sizeof(ch[0])); }    //初始化时只有一个根结点
12      int idx(char c) { return c-'a';}
13      void insert(char *s,int v){    //插入字符串
14          //插入字符串是,附加信息为v。注意v必须为非0,因为0代表“本结点不是单词结点”
15          int u=0,n=strlen(s);
16          for(int i=0;i<n;i++){
17              int c=idx(s[i]);
18              if(!ch[u][c]){        //结点不存在
19                  memset(ch[sz],0,sizeof(ch[sz]));
20                  val[sz]=0;        //中间结点的附加信息为0
21                  ch[u][c] = sz++;    //新建结点
22              }
23              u=ch[u][c];        //往下走
24          }
25          val[u] = v;        //字符串最后一个字符的附加信息为v
26      }
27      int find(char *s){        //查找字符串
28          int u=0,n=strlen(s);
29          for(int i=0;i<n;i++){
30              int c=idx(s[i]);
31              if(!ch[u][c]){
32                  return 0;
33              }
34              u=ch[u][c];
35          }
36          return val[u];
37      }
38  }s;
39  int main(){
40      int num=1,i,j;
41      char ss[30];    //要查找字符串
42      char s1[30],s2[30],s3[60]; 
43      //s1英语,s2方言,s3为了检查字典写完而存在的
44      //输入字典
45      while(1){
46          gets(s3);
47          int len = strlen(s3);
48          if(len==0)
49              break;
50          int flag=0;
51          for(i=0,j=0;i<len;i++){
52              if(s3[i]==' ') {s1[j]=0; flag=1; j=0; continue;}
53              if(flag==0)
54                  s1[j++]= s3[i];
55              else
56                  s2[j++] = s3[i];
57          }
58          s2[j]=0;
59          strcpy(ans[num],s1);    
60          s.insert(s2,num);
61          num++;
62      }
63  //查找单词
64      while(scanf("%s",ss)!=EOF){
65          if(s.find(ss)==0)    //没找到
66              printf("eh\n");
67          else
68          {
69              printf("%s\n",ans[s.find(ss)]);
70          }
71      }
72      return 0;
73  }
View Code

 

 

你可能感兴趣的:(poj)