[1441] Babelfish noj(宁波)

http://ac.nbutoj.com/Problem/view.xhtml?id=1441

  • [1441] Babelfish

  • 时间限制: 1000 ms 内存限制: 65535 K
  • 问题描述
  • 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 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 is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as "eh".
  • 样例输入
  • dog ogday
    cat atcay
    pig igpay
    froot ootfray
    loops oopslay
    
    atcay
    ittenkay
    oopslay
    
  • 样例输出
  • cat
    eh
    loops
  •  1 #include<math.h>
     2 #include<algorithm>
     3 #include<string.h>
     4 #include<string>
     5 #include<ctime>
     6 #include<queue>
     7 #include<list>
     8 #include<map>
     9 #include<set>
    10 #include<vector>
    11 #include<stack>
    12 #include<iostream>
    13 #define INF 999999999
    14 #define N 50010
    15 using namespace std;
    16 
    17 int cnt=1;
    18 typedef struct node
    19 {
    20     node * next[26];
    21     int num;
    22     node()
    23     {
    24         num=0;
    25         memset(next,NULL,sizeof(next));
    26     }
    27 }Nod;
    28 void insert_tree(Nod * head,char *str)
    29 {
    30     Nod *h=head;
    31     int len=strlen(str);
    32     int i;
    33     for(i=0;i<len;i++)
    34     {
    35         int id=str[i]-'a';
    36         if(h->next[id]==NULL)
    37             h->next[id]=new node;
    38         h=h->next[id];
    39     }
    40     h->num=cnt++;   //这个标记结尾,并标记是第几个,作为hash
    41 }
    42 int find_tree(Nod *head,char *str)
    43 {
    44     int i;
    45     Nod *h=head;
    46     int len=strlen(str);
    47     for(i=0;i<len;i++)
    48     {
    49         int id=str[i]-'a';
    50         if(h->next[id]==NULL)
    51             return 0;     //没找到则返回0
    52         h=h->next[id];
    53     }
    54     if(h!=NULL)
    55         return h->num;  //当h不为空,返回hash
    56     return 0;
    57 }
    58 void freedom(Nod *h)  //这是对指针的释放,这个题不知道怎么回事,释放会超时,估计是因为递归释放耗时太多,不过题目只有一组测试,可以不释放
    59 {
    60     int i;
    61     for(i=0;i<26;i++)
    62     {
    63         if(h->next[i]!=NULL)
    64             freedom(h->next[i]);
    65     }
    66     delete h;
    67 }
    68 
    69 char tstr[100010][20]={"eh",}; //初始化hash字符数组
    70 
    71 int main()
    72 {
    73     char str[20];
    74     int i=1,n;
    75     cnt=1;
    76     Nod *head;
    77     head=new node;
    78     char ch;
    79     while(~scanf("%s",tstr[i]))
    80     {
    81         ch=getchar();
    82         if(ch==10)    //输入这里我用了getchar来查看字符串后面的字符是不是回车
    83             break;
    84         scanf("%s",str);
    85         insert_tree(head,str);
    86         i++;
    87     }
    88     int pos=find_tree(head,tstr[i]);
    89     puts(tstr[pos]);
    90     while(~scanf("%s",str))
    91     {
    92         pos=find_tree(head,str);
    93         puts(tstr[pos]);
    94     }
    95     //freedom(head);
    96     return 0;
    97 }
原文地址:https://www.cnblogs.com/crazyapple/p/3045740.html