zoukankan      html  css  js  c++  java
  • UVA 11732 strcmp() Anyone? (压缩版字典树)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2832

    按照正常的字典树建树会MLE

    所以需要采用树的压缩算法来建树

    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #define maxn 4000010
    #define maxl 1010
    using namespace std;
    
    typedef long long LL;
    
    typedef struct _node {
        char value;
        int size;
        int count;
        _node* lchild;
        _node* rchild;
    } node;
    
    node dict[maxn];
    char str[maxl];
    
    class Trie
    {
        private:
            LL count;
            int size;
            node* root;
            
        public:
            
            Trie()
            {
                init();
            }
            
            void init()
            {
                memset( dict, 0, sizeof(dict));
                size = 0;
                count = 0;
                root = new_node(0);
            }
            
            node* new_node(char c)
            {
                dict[size].value = c;
                return &dict[size++];
            }
            
            void insert(char* word)
            {
                int len = (int)strlen(word), same = 1;
                node* now = root->rchild;
                node* pa = root;
                for (int i = 0; i <= len; ++i)
                {
                    if (!now)
                    {
                        pa->rchild = new_node( word[i]);
                        now = pa->rchild;
                        now->count = 1;
                        same = 0;
                    }
                    else
                    {
                        if (i)
                            count += now->count;
                        count += now->count++;
                        
                        while (now->lchild && now->value != word[i])
                            now = now->lchild;
                        if (now->value != word[i])
                        {
                            now->lchild = new_node( word[i]);
                            now = now->lchild;
                            same = 0;
                        }
                    }
                    pa = now;
                    now = pa->rchild;
                }
                
                if (same)
                    pa->size++;
                count += pa->size;
            }
            
            LL query()
            {
                return count;
            }
    } trie;
    
    int main(void)
    {
        int ca = 1, n;
        while (scanf("%d", &n), n)
        {
            Trie trie = Trie();
            for (int i = 0; i < n; ++i)
            {
                scanf("%s", str);
                trie.insert( str);
            }
            printf("Case %d: %lld\n", ca++, trie.query());
        }
        return 0;
    }
  • 相关阅读:
    仿google的suggest
    renderjs主动发起通讯
    数据库设计原则(转载)
    SqlBulkCopy批量插入
    NPOI使用word模板
    visual studio 2010与NUNIT集成调试
    bond
    linux 内核参数优化
    Linux清除用户登陆及历史命令
    nginx 常用请求
  • 原文地址:https://www.cnblogs.com/chuninsane/p/4924239.html
Copyright © 2011-2022 走看看