题目链接: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. IOS简单的字串替换方法stringByTrimmingCharactersInSet 和空格的替换 ()

    NSString *str = @"一个 (ABC) #@!#^#G@#Dd21e12d!E@!212s012yhsn 汉0932字#@!中..文//>?输~~~@#$#@%#^#^% ...

  2. express html模板项目搭建

    初学express的亲们,估计要弄ejs和jade会比较烦躁,那就先html开始,简单笔记如下:   1.新建项目文件夹demotest 2.进入demotest    >express -e ...

  3. ArrayList、HashTable、List、Dictionary的演化及如何选择使用

    在C#中,数组由于是固定长度的,所以常常不能满足我们开发的需求. 由于这种限制不方便,所以出现了ArrayList. ArrayList.List<T> ArrayList是可变长数组,你 ...

  4. python数据类型及其常用方法

    1.整型 int 在32位机器上,整数的位数为32位,取值范围为-2**31-2**31-1,即-2147483648-2147483647 在64位系统上,整数的位数为64位,取值范围为-2**63 ...

  5. Mybatis 插入null值报错

    解决方法: 1.在settings中配置 <setting name="jdbcTypeForNull" value="OTHER"/> MyBat ...

  6. 服务器响应头隐藏X-power-by

    服务器响应头隐藏X-power-by 这个不是Apache或者Nginx输出的,而是由语言解析器或者应用程序框架输出的. 这个值的意义用于告知网站是用何种语言或框架编写的.例如: PHP标准输出是:X ...

  7. c++的类与对象

    对象:此对象,非彼对象,:-D,跟妹子无关(不过貌似也可以,,),闲言少叙,书归正传 我们可以把我们见到的一切事物都称为对象.它可以有形,可以无形,可以简单,可以复杂,但是对某一种具体的对象,比如公司 ...

  8. MongoDB-MMS使用总结

    环境:阿里云 系统:ubuntu 12.04 数据库:MongoDB shell version: 2.0.4 登录MMS,注册相应用户 根据文档开始安装:Install the Monitoring ...

  9. js生成二维码实例(真实有效)

    js文件  qrcode.js   代码 /*from tccdn minify at 2014-6-4 14:59:43,file:/cn/c/c/qrcode.js*/ /** * @fileov ...

  10. React学习之一:React初探

    一,React简介 React是由Facebook和Instagram开发的一套创建用户界面的JavaScript库.许多人认为React是MVC中的V. React创建的目的是为了:构建数据随时会改 ...