zoukankan      html  css  js  c++  java
  • POJ_2503 Babelfish 字典树

    题目链接:http://poj.org/problem?id=2503

    依旧字典树

    注意新建节点初始化值,读取数据的时候可以用gets判空行,最后删除分配的空间防RE

    评论区还有用map过的,比较耗时。

    代码:

     1 #include <cstdio>
     2 #include <cstdlib>
     3 #include <iostream>
     4 #include <cstring>
     5 #include <string>
     6 using namespace std;
     7 #define maxnode 27
     8 #define maxn 100005
     9 struct trie{
    10     trie *next[maxnode];
    11     int index;
    12 };
    13 trie * root;
    14 char st[maxn][maxnode];
    15 
    16 trie * newnode(){
    17     trie *p = (trie *)malloc(sizeof(trie));
    18     p->index = 0;
    19     for(int i = 0; i < maxnode; i++)
    20         p->next[i] = 0;
    21     return p;
    22 }
    23 void init(){
    24     root = newnode();
    25     root->index = 0;
    26     for(int i = 0; i < maxnode; i++)
    27         root->next[i] = 0;
    28 }
    29 int insert_node(char tms[], int sindex){
    30     trie * p = root, *q;
    31     int len = strlen(tms), i = 0;
    32     for(; i < len; i++){
    33         if(p->next[tms[i] - 'a'])
    34             p = p->next[tms[i] - 'a'];
    35         else{
    36             q = newnode();
    37             p->next[tms[i] - 'a'] = q;
    38             p = q;
    39         }
    40     }
    41     p->index = sindex;
    42     return 1;
    43 }
    44 // -1 == No Found
    45 int find_node(char tms[]){
    46     trie *p = root;
    47     int len = strlen(tms);
    48     for(int i = 0; i < len; i++){
    49         if(p->next[tms[i] - 'a'])
    50             p = p->next[tms[i] - 'a'];
    51         else
    52             return -1;
    53     }
    54     if(p->index){
    55         return p->index;
    56     }
    57     return -1;
    58 }
    59 void deltrie(trie *node){
    60     if(node){
    61         for(int i = 0; i < maxnode; i++){
    62             deltrie(node->next[i]);
    63         }
    64         free(node);
    65     }
    66 }
    67 
    68 int main(){
    69 
    70     char tms[maxnode];
    71     int totid = 1;
    72     init();
    73     while(gets(tms) && strcmp(tms, "")){
    74         char tps[maxnode];
    75         sscanf(tms, "%s %s", st[totid], tps);
    76         //printf("
    -->totid: %d<--
    ", totid);
    77         insert_node(tps, totid);
    78         totid++;
    79     }
    80     while(gets(tms) && strcmp(tms, "")){
    81         int tp = find_node(tms);
    82         if(tp == -1){
    83             printf("eh
    ");
    84         }
    85         else{
    86             //printf("-->tp: %d-->tms: %s
    ", tp, tms);
    87             printf("%s
    ", st[tp]);
    88         }
    89     }
    90     deltrie(root);
    91 }

    题目:

    Babelfish
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 43616   Accepted: 18403

    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
    

    Hint

    Huge input and output,scanf and printf are recommended.
  • 相关阅读:
    LVS三种模式配置及优点缺点比较
    LVS负载均衡-DR模式
    Nginx反向代理和负载均衡
    LNMP(centos7.6 Nginx1.16 MySQL5.7 PHP7 )
    Nginx常用模块
    表演中的晴天
    迷雾中前行
    mysql Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nona
    npm install -g cnpm --registry=https://registry.npm.taobao.org
    ubuntu16 安装elasticsearch6.3问题
  • 原文地址:https://www.cnblogs.com/bolderic/p/6800736.html
Copyright © 2011-2022 走看看