题目链接: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, , sizeof(dict));
size = ;
count = ;
root = new_node();
} node* new_node(char c)
{
dict[size].value = c;
return &dict[size++];
} void insert(char* word)
{
int len = (int)strlen(word), same = ;
node* now = root->rchild;
node* pa = root;
for (int i = ; i <= len; ++i)
{
if (!now)
{
pa->rchild = new_node( word[i]);
now = pa->rchild;
now->count = ;
same = ;
}
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 = ;
}
}
pa = now;
now = pa->rchild;
} if (same)
pa->size++;
count += pa->size;
} LL query()
{
return count;
}
} trie; int main(void)
{
int ca = , n;
while (scanf("%d", &n), n)
{
Trie trie = Trie();
for (int i = ; i < n; ++i)
{
scanf("%s", str);
trie.insert( str);
}
printf("Case %d: %lld\n", ca++, trie.query());
}
return ;
}

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

  1. UVA 11732 - strcmp() Anyone?(Trie)

    UVA 11732 - strcmp() Anyone? 题目链接 题意:给定一些字符串,要求两两比較,须要比較的总次数(注意.假设一个字符同样.实际上要还要和'\0'比一次,相当比2次) 思路:建T ...

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

    题目地址: option=com_onlinejudge&Itemid=8&category=117&page=show_problem&problem=2832&qu ...

  3. UVA 11488 Hyper Prefix Sets (字典树)

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

  4. Uva 11732 strcmp()函数

    题目链接:https://vjudge.net/contest/158125#problem/A 题意: 系统中,strcmp函数是这样执行的,给定 n 个字符串,求两两比较时,strcmp函数要比较 ...

  5. uva 11488 - Hyper Prefix Sets(字典树)

    H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of str ...

  6. UVA 11732 - strcmp() Anyone? 字典树

    传送门:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  7. UVa 11732 "strcmp()" Anyone? (左儿子右兄弟前缀树Trie)

    题意:给定strcmp函数,输入n个字符串,让你用给定的strcmp函数判断字符比较了多少次. 析:题意不理解的可以阅读原题https://uva.onlinejudge.org/index.php? ...

  8. UVA - 11732 "strcmp()" Anyone? (trie)

    https://vjudge.net/problem/UVA-11732 题意 给定n个字符串,问用strcmp函数比较这些字符串共用多少次比较. strcmp函数的实现 int strcmp(cha ...

  9. Uva 11732 strcmp() Anyone?

    strcmp() Anyone? Time Limit: 2000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [Subm ...

随机推荐

  1. Android UI 绘制过程浅析(三)layout过程

    前言 上一篇blog中,了解到measure过程对View进行了测量,得到measuredWidth/measuredHeight.对于ViewGroup,则计算出全部children的宽高进行求和. ...

  2. appium入门

    前语:学习需要总结,或许有些知识自己存在偏差,但是能总结出来就会更加加深所学知识 1.       环境变量配置 必备软件安装: jdk1.6.0 android-sdk python:2.7(3.6 ...

  3. Home not found. Define system property "openfireHome" or create and add the openfire_init.xml file to the classpath

    启动openfire后出现这个错误,貌似什么配置没对吧.网上搜索了下,找到解决办法, $ vi /etc/profile在里面加入:export openfireHome=/opt/openfire ...

  4. spring 获取 bean

    不通过注解或者是配置文件怎么获取spring中定义的bean呢?有几个方法: 1.实现ApplicationContextAware <bean class="com.xxx.Spri ...

  5. 炫酷的时钟--canvas初体验

    先啥也不说:来张效果图 我是根据:http://www.imooc.com/learn/133 这里的课程进行学习的.大大的感谢liuyubobobo老师的深入浅出的讲解!! 我在这里仅仅提供我自己的 ...

  6. Python str方法总结

    1.返回第一个字母大写 S.capitalize(...) S.capitalize() -> string 1 2 3 4 >>>a = 'shaw' >>> ...

  7. 排序算法 2 qsort 库函数,泛型函数

    _____谈谈排序算法 交换排序——>冒泡排序-->快速排序 选择排序——>简单选择排序——>堆排序 插入排序——>直接插入排序——>希尔排序 _____排序算法对 ...

  8. memory allocation

    1 malloc与free是C++/C语言的标准库函数,new/delete是C++的运算符. 2,malloc, 必须 包含头文件<stdlib.h> 3, nt* p_scalar = ...

  9. nagios二次开发(二)---nagios和nagiosql合并与取舍

    NAGIOS做前台 上一篇本人分析了nagios和nagiosql的优缺点,根据之前的使用经验及探索.决定将nagios做为监控数据的展示层,暂称做“前台”.将nagiosql做为监控体的配置层,暂称 ...

  10. DDK Build的DIRS和SOURCE文件

    DDK Build编译的时候,使用3个文件来描述被编译的源码,其中SOURCES和Makefile是必须的,而DIRS则只在划分目录的时候有用.Makefile在这里作用并不大但是必须和SOURCES ...