Babelfish

来源:互联网 发布:数学建模的算法 编辑:程序博客网 时间:2024/06/05 09:33

Babelfish

题目描述:

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. 

题目解析:

三种方法:
一:map映射
二:字典树
三:ELFhash

第一种:map映射

map映射是理解上最为简单的算法,但是这题的map映射需要注意一个地方就是虽然在声明map的时候是用的“map < string , string > ”,但是我们在访问和赋值时却不能使用string类型进行操作,这样会超时,所以应该使用char型的指针来传递就好了。

代码如下:

#include<stdio.h>  #include<iostream>  #include<string.h>  #include<stdlib.h>  #include<string>  #include<map>  using namespace std;  map<string,string>Map;  char a[25];  char s[25];  char d[25];  char b[25];  void init()  {      Map.clear();      }  int main()  {      int i,j;      while(gets(a))      {          if(a[0] == '\0')              break;          int len = strlen(a);          for(i=0; i<len; i++)          {              if(a[i] == ' ')                  break;          }          int k=0;           for(j=0; j<i; j++)              s[k++] = a[j];          k = 0;          for(j=i+1; j<len; j++)              d[k++] = a[j];          Map[d] = s;      }      while(gets(b))      {          if(Map.find(b)!=Map.end())              cout << Map[b] <<endl;          else              puts("eh");       }      return 0;  }

第二种:字典树

字典树是一种运用于字符串的插入,查找,删除的数据结构。他利用了字符串的公共前缀来储存,节约了极大的空间,减少了很多字符的无谓比较,速度比hash表快。字典树其实和我们平时查字典是差不多,不断寻找匹配前缀。

其基本性质可以归纳为:

  1. 根节点不包含字符,除根节点外每一个节点都只包含一个字符。

  2. 从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串。

  3. 每个节点的所有子节点包含的字符都不相同。

而查询的操作步骤则分为一下几步:

(1) 从根结点开始一次搜索;

(2) 取得要查找关键词的第一个字母,并根据该字母选择对应的子树并转到该子树继续进行检索;

(3) 在相应的子树上,取得要查找关键词的第二个字母,并进一步选择对应的子树进行检索。

(4) 迭代过程……

(5) 在某个结点处,关键词的所有字母已被取出,则读取附在该结点上的信息,即完成查找。

本题只要再字典树的叶子节点储存好对应的翻译单词,查到的时候输出就好。

代码如下:

#include <string>#include <cstdio>#include <cstring>#include <cstdlib>#include <iostream>using namespace std;const int MAX = 100010;char s[MAX][15],c[15];struct node{    int  flag;    node *next[26];}*head;int top;node * Creat(){    node *p;    p=new node;    p->flag=0;    for(int i=0;i<26;i++)    {        p->next[i]=NULL;    }    return p;}void Build_Tree(){    node *p=head;    int a,len=strlen(c);    for(int i=0;i<len;i++)    {        a=c[i]-'a';        if(!p->next[a])        {            p->next[a]=Creat();        }        p=p->next[a];    }    p->flag=top;//将对应单词的编号赋给它,便于查找}int Look(){    node *p=head;    int a,len=strlen(c);    for(int i=0;i<len;i++)    {        a=c[i]-'a';        if(!p->next[a])        {            return 0;        }        p=p->next[a];    }    if(!p->flag)//查找不到返回零    {        return 0;    }    else    {        return p->flag;    }}int main(){    head=Creat();    top=1;    int t;    char cc[30];    while(gets(cc)&&cc[0]!='\0')    {        sscanf(cc,"%s %s",s[top],c);        Build_Tree();        top++;    }    while(~scanf("%s",c))    {        t=Look();        if(t)        {            printf("%s\n",s[t]);        }        else        {            printf("eh\n");        }    }    return 0;}

第三种:ELFhash

没搞懂。

原创粉丝点击