• POJ 2503 Trie


    链接:

    http://poj.org/problem?id=2503

    题意:

    给定一些字符串以及它在外星语言中的对应翻译,现在有若干外星语言中的串,要把它们翻译成英语

    题解:

    这道题map,hash,trie都是可以做的

    然而我用g++提交发现map和trie都超时了,换成c++就全都过了

    map用了1141ms,trie只要485ms

    代码:

    31 vector<string> word;
    32 int pi =1;
    33 
    34 struct Node {
    35     int next[26];
    36     int value;
    37     bool end;
    38 }tree[MAXN * 10];
    39 
    40 void insert(string keyword, int value) {
    41     int index, p, i;
    42     for (i = p = 0; keyword[i]; i++) {
    43         index = keyword[i] - 'a';
    44         if (tree[p].next[index] == 0)
    45             tree[p].next[index] = pi++;
    46         p = tree[p].next[index];
    47     }
    48     tree[p].value = value;
    49     tree[p].end = 1;
    50 }
    51 
    52 int query(string keyword) {
    53     int index, p, i;
    54     for (i = p = 0; keyword[i]; i++) {
    55         index = keyword[i] - 'a';
    56         if (tree[p].next[index] == 0) return 0;
    57         p = tree[p].next[index];
    58     }
    59     if (tree[p].end) return tree[p].value;
    60     return 0;
    61 }
    62 
    63 int main() {
    64     string s, a, b;
    65     word.pb("eh");
    66     while (getline(cin, s) && s != "") {
    67         int fg = 1;
    68         rep(i, 0, s.length()) {
    69             if (s[i] == ' ') fg = 0;
    70             else if (fg) a += s[i];
    71             else b += s[i];
    72         }
    73         word.pb(a);
    74         insert(b, word.size() - 1);
    75         a = b = "";
    76     }
    77     while (cin >> a) cout << word[query(a)] << endl;
    78     return 0;
    79 }
  • 相关阅读:
    EF在二手市场中的使用
    二手商城集成jwt认证授权
    core3商城DDD实战(一)建模
    数组的逆序对
    C++实现线程安全的单例
    分配格充要条件的两种证明
    一个简单的日历系统(C++)
    HTTP基础--网页基础
    HTTP基础 --响应
    HTTP基础--请求
  • 原文地址:https://www.cnblogs.com/baocong/p/6758996.html
Copyright © 2020-2023  润新知