题目链接: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 Studio JNI/NDK 编程(二) Windows 下环境搭建 demo 开发

    环境 windows 8  (注:其实 Linux 开发可能更方便) Android Studio 2.1; 一 . 下载 安装android-ndk开发包 地址:链接:http://pan.baid ...

  2. Android Menu 主菜单是使用

    在Android里面每个Activity都绑定了一个Menu(菜单)有些时候我们需要使用到菜单.用法很简单,我们只需要重写onCreateOptionsMenu和onOptionsItemSelect ...

  3. 纸上谈兵:AVL树

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 二叉搜索树的深度与搜索效率 我们在树, 二叉树, 二叉搜索树中提到,一个有n个节点 ...

  4. tcp协议-http协议-time-wait-close-wait必知

     前言:  tcp四次挥手过程中,谁主动断开,谁有time_wait,被动断开一方会有close_wait time_wait:保持端口占用2mls~4min,避免对方还有一些tcp片发往这个端口,新 ...

  5. const int * p 和 int const * p 和 int * const p 的区别

    首先注意,const int * p 和int const *p 是一样的,并且不管是不是*p,即使const int i和int const i也是一样的,所以我们接下来只讨论int const * ...

  6. javascript关于闭包变量作用域

    在项目中不时会遇到的一些小的问题以及解决办法: 1子函数调用父函数中的变量: 加return: var a=1; function num(){ var b=2; return b; } num()+ ...

  7. Devexpress VCL Build v2015 vol 15.2 开始测试

    增加了几个小玩意,与大版本变化根本无法匹配. 具体可以官网了解 https://www.devexpress.com/Subscriptions/New-2015.xml?product=vcl

  8. 移动端WEB开发,click,touch,tap事件浅析

    一.click 和 tap 比较 两者都会在点击时触发,但是在手机WEB端,click会有 200~300 ms,所以请用tap代替click作为点击事件. singleTap和doubleTap 分 ...

  9. Python list方法总结

    1. 向列表的尾部添加一个新的元素 append(...) L.append(object) -- append object to end 1 2 3 4 >>> a = ['sa ...

  10. .net正则表达式

    1. "^-?[1-9]\\d*$",//整数 2. "^[1-9]\\d*$", //正整数 3. intege2:"^-[1-9]\\d*$&qu ...