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;
}
时间: 2024-08-24 04:59:16

UVA 11732 strcmp() Anyone? (压缩版字典树)的相关文章

uva 11732 - strcmp() Anyone?(字典树)

题目链接:uva 11732 - strcmp() Anyone? 题目大意:给定n个串,然后两两之间比较,问说总共要比较多少次. 解题思路:字典树,建立出字典树,然后根据字典树的性质在节点记录有多少个字符串包含该节点.因为节点的个数比较多,所以用左孩子右兄弟的方法建立字典树. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long

UVa - 11732 - strcmp() Anyone?

先上题目: strcmp() Anyone? Time Limit:2000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description J ?strcmp()? Anyone? Input: Standard Input Output: Standard Output strcmp() is a library function in C/C++ which compares two stri

UVA 11732 - strcmp() Anyone?(Trie)

UVA 11732 - strcmp() Anyone? 题目链接 题意:给定一些字符串,要求两两比较,需要比较的总次数(注意,如果一个字符相同,实际上要还要和'\0'比一次,相当比2次) 思路:建Trie树,每次建树过程中,后继后继结点就是相同结点需要比较两次ans + val * 2,否则就是不同结点ans + val,建完树就计算完了 代码: #include <cstdio> #include <cstring> const int N = 1005; const int

左儿子右兄弟Trie UVA 11732 strcmp() Anyone?

UVA 11732 strcmp() Anyone?(左儿子右兄弟Trie) ACM 题目地址: UVA 11732 strcmp() Anyone? 题意: 问strcmp函数的==语句执行了几次. UVA 11732 strcmp() Anyone?(左儿子右兄弟Trie) ACM 题目地址: UVA 11732 strcmp() Anyone? 题意: 问strcmp函数的==语句执行了几次. 分析: 大白上的题目. 听说要用左儿子右兄弟的Trie,比较省空间,顺便学了下. 一边inser

uva 11488 - Hyper Prefix Sets(字典树)

H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of strings in the set. For example the prefix goodness of the set {000,001,0011} is 6.You are given a set of binary strings. Find the maximum prefix goodnes

uva 11732 - strcmp() Anyone? 不错的Trie题

题解:http://blog.csdn.net/u013480600/article/details/23122503 我的代码一直TLE,,,看了人家的之后,觉得1.链式前向星比较好,2.*depth而不是每过一个节点就计算,这一点很好 我是基本copy别人的代码,自己加了注释,留个记号,随后重写, 这道题同样作为链式前向星的Trie的模板 #include <cstdio> #include <cstring> #include <iostream> using n

UVA 11732 strcmp() Anyone?(左儿子右兄弟Trie)

UVA 11732 strcmp() Anyone?(左儿子右兄弟Trie) ACM 题目地址: UVA 11732 strcmp() Anyone? 题意: 问strcmp函数的==语句执行了几次. 分析: 大白上的题目. 听说要用左儿子右兄弟的Trie,比较省空间,顺便学了下. 开始先建树记录次数,然后再遍历统计,结果错了... 后面参考了Shoutmon巨巨的写法,一边insert一边统计. 代码: /* * Author: illuz <iilluzen[at]gmail.com> *

UVa 11732 &quot;strcmp()&quot; Anyone? (左儿子右兄弟前缀树Trie)

题意:给定strcmp函数,输入n个字符串,让你用给定的strcmp函数判断字符比较了多少次. 析:题意不理解的可以阅读原题https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2832 字符串很多,又很长,如果按照题目的意思两两比较,肯定会TLE,所以要用前缀树(Trie)来解决,当然只是用简单的前缀树也会TLE的, 我们必须对其进行优化,看了

UVA 11732 &quot;strcmp()&quot; Anyone? (Trie)

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2832 题意:模拟strcmp()函数,给n个字符串,两两之间进行比较,问需要进行多少次比较? 解析:字符串很多,数据量大,按题意两两比较显然不现实.如果把所有的单词插入到一棵Trie里面会怎么样呢?考虑than和that对应的结点,than和that的前3个字符是一样的,但第4个