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, , 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? (压缩版字典树)的更多相关文章
- UVA 11732 - strcmp() Anyone?(Trie)
UVA 11732 - strcmp() Anyone? 题目链接 题意:给定一些字符串,要求两两比較,须要比較的总次数(注意.假设一个字符同样.实际上要还要和'\0'比一次,相当比2次) 思路:建T ...
- 左儿子右兄弟Trie UVA 11732 strcmp() Anyone?
题目地址: option=com_onlinejudge&Itemid=8&category=117&page=show_problem&problem=2832&qu ...
- UVA 11488 Hyper Prefix Sets (字典树)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- Uva 11732 strcmp()函数
题目链接:https://vjudge.net/contest/158125#problem/A 题意: 系统中,strcmp函数是这样执行的,给定 n 个字符串,求两两比较时,strcmp函数要比较 ...
- uva 11488 - Hyper Prefix Sets(字典树)
H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of str ...
- UVA 11732 - strcmp() Anyone? 字典树
传送门:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- UVa 11732 "strcmp()" Anyone? (左儿子右兄弟前缀树Trie)
题意:给定strcmp函数,输入n个字符串,让你用给定的strcmp函数判断字符比较了多少次. 析:题意不理解的可以阅读原题https://uva.onlinejudge.org/index.php? ...
- UVA - 11732 "strcmp()" Anyone? (trie)
https://vjudge.net/problem/UVA-11732 题意 给定n个字符串,问用strcmp函数比较这些字符串共用多少次比较. strcmp函数的实现 int strcmp(cha ...
- Uva 11732 strcmp() Anyone?
strcmp() Anyone? Time Limit: 2000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu [Subm ...
随机推荐
- JAVA设计模式之解释器模式
在阎宏博士的<JAVA与模式>一书中开头是这样描述解释器(Interpreter)模式的: 解释器模式是类的行为模式.给定一个语言之后,解释器模式可以定义出其文法的一种表示,并同时提供一个 ...
- 埃及分数-IDA*
Description 在古埃及,人们使用单位分数的和(形如1/a的, a是自然数)表示一切有理数.如:2/3=1/2+1/6,但不允许2/3=1/3+1/3,因为加数中有相同的.对于一个分数a/b, ...
- iOS开发--应用设置及用户默认设置【1、bundle的运用】
在iphone里面,应用都会在“设置”里面有个专属的应用设置,选择该菜单界面,用户便可以在其中输入和更改各种选项,协助用户更便捷设置个人喜好与习惯. 在这一节中,希望能通过对捆绑包(bu ...
- 用通俗易懂的大白话讲解Map/Reduce原理
Hadoop简介 Hadoop就是一个实现了Google云计算系统的开源系统,包括并行计算模型Map/Reduce,分布式文件系统HDFS,以及分布式数据库Hbase,同时Hadoop的相关项目也很丰 ...
- Ubuntu14.04通过pyenv配置多python
参考链接: https://github.com/yyuu/pyenv-virtualenv https://github.com/yyuu/pyenv http://seisman.info/pyt ...
- jquery validate ajax submit form
when the jquery validation plugin is used for validating the form data, such as below: html code: &l ...
- WebForm---增删改(内置对象)
一.添加 前台代码: <body> <form id="form1" runat="server"> <h1>用户添加< ...
- codeforces 361 E - Mike and Geometry Problem
原题: Description Mike wants to prepare for IMO but he doesn't know geometry, so his teacher gave him ...
- MVC dirname(——FILE——)
1.MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数据.界面显示分离的方法组织 ...
- 删除多余的provisioning profile
当你的工程中有个人证书和企业证书时,你xCode打包的时候可能会发现一个问题:当你的Code Signing设置iOS Developer和Automatic,并且你的Bundle ID和Team选择 ...