codeforces 965E Trie+multiset
1 second
256 megabytes
standard input
standard output
Arkady's code contains nn variables. Each variable has a unique name consisting of lowercase English letters only. One day Arkady decided to shorten his code.
He wants to replace each variable name with its non-empty prefix so that these new names are still unique (however, a new name of some variable can coincide with some old name of another or same variable). Among such possibilities he wants to find the way with the smallest possible total length of the new names.
A string aa is a prefix of a string bb if you can delete some (possibly none) characters from the end of bb and obtain aa.
Please find this minimum possible total length of new names.
The first line contains a single integer nn (1≤n≤1051≤n≤105) — the number of variables.
The next nn lines contain variable names, one per line. Each name is non-empty and contains only lowercase English letters. The total length of these strings is not greater than 105105. The variable names are distinct.
Print a single integer — the minimum possible total length of new variable names.
3
codeforces
codehorses
code
6
5
abba
abb
ab
aa
aacada
11
3
telegram
digital
resistance
3
In the first example one of the best options is to shorten the names in the given order as "cod", "co", "c".
In the second example we can shorten the last name to "aac" and the first name to "a" without changing the other names.
题意:给出很多字符串(1e5),总长度不超过1e5.
每个字符串用其某前缀代替,代替后要保证每个字符串互不相同。
求代替后最短的总距离。
题解:
做法非常巧妙和套路
建个Trie树,每个节点维护深度和一个multiset用于存储深度(当前取得字符串前缀的长度)(也可以用priority_queue,这种做法明天补上)
初始时,叶节点的multiset存储该节点的深度,这个初始状态就是最差的情况,即每个字符串都保留原串,下面会在合并的时候进行统计和优化。
然后从根节点开始dfs,dfs回溯段合并子节点的multiset,合并之后,去掉multiset中的最大值,插入当前节点深度(意义就是将当前最长的前缀替换成当前节点所代表的前缀,这样贪心一定是最优的)。
最后遍历根节点的multiset就可以得到答案。
合并的复杂度是
这样的,根据主定理,就是mlogm
,multiset的操作的复杂度是logm,总复杂度是m*(logm)^2
合并之后一定要把子节点的multiset清空,不然会MLE。
/*
◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇
◇◇◇◇◇◇◆◆◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◆◇◇◇◇◇◇◇◇◇
◇◇◇◇◇◇◆◆◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◆◆◇◇◇◇◇◇◇◇◇
◇◇◇◇◇◇◇◆◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◆◇◇◇◇◇◇◇◇◇
◇◇◇◇◇◇◇◆◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◆◇◆◆◆◇◇◇◇◇
◇◇◇◇◇◇◇◆◇◇◇◇◇◇◇◇◇◇◇◇◆◆◆◆◆◇◇◇◇◇◇◇◇◇◇◆◆◆◆◆◇◇◇◇◇
◇◇◇◇◇◇◇◆◇◇◇◇◇◇◇◇◇◇◇◇◆◆◆◆◆◇◇◇◇◇◇◇◇◇◇◆◆◇◆◆◇◇◇◇◇
◇◇◇◇◇◇◇◆◇◇◇◇◇◇◇◇◇◇◇◇◇◇◆◆◇◇◇◇◇◇◇◇◇◇◇◆◇◇◇◆◇◇◇◇◇
◇◇◇◇◇◇◇◆◇◇◇◇◇◇◇◇◇◇◇◇◇◆◆◇◇◇◇◇◇◇◇◇◇◇◇◆◇◇◇◆◇◇◇◇◇
◇◇◇◇◇◇◇◆◇◇◇◇◇◇◇◇◇◇◇◇◆◆◆◇◆◇◇◇◇◇◇◇◇◇◇◆◇◇◇◆◇◇◇◇◇
◇◇◇◇◇◇◆◆◆◆◇◇◇◇◇◇◇◇◇◇◆◆◆◆◆◇◇◇◇◇◇◇◇◇◆◆◆◇◆◆◆◇◇◇◇
◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇
◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇
◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇
◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇◇
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<set>
#include<vector>
using namespace std;
int read(){
int xx=,ff=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')ff=-;ch=getchar();}
while(ch>=''&&ch<=''){xx=xx*+ch-'';ch=getchar();}
return xx*ff;
}
const int maxn=;
int N;
char str[maxn];
struct Trie{
int lin[];
int depth;
multiset<int>s;
}T[maxn];
int cnt=;
void insert(int depth,int root){
if(!str[depth]){
T[root].s.insert(T[root].depth);
return;
}
if(!T[root].lin[str[depth]-'a']){
T[root].lin[str[depth]-'a']=++cnt;
T[cnt].depth=T[root].depth+;
}
insert(depth+,T[root].lin[str[depth]-'a']);
}
void trav(int x){
bool flag=x&&T[x].s.empty();
for(int i=;i<;i++)
if(T[x].lin[i]){
trav(T[x].lin[i]);
if(T[x].s.size()<T[T[x].lin[i]].s.size())
swap(T[x].s,T[T[x].lin[i]].s);
for(multiset<int>::iterator it=T[T[x].lin[i]].s.begin();it!=T[T[x].lin[i]].s.end();it++)
T[x].s.insert(*it);
T[T[x].lin[i]].s.clear();
}
if(flag){
T[x].s.erase(--T[x].s.end());
T[x].s.insert(T[x].depth);
}
}
int main(){
//freopen("in","r",stdin);
N=read();
for(int i=;i<=N;i++){
gets(str);
insert(,);
}
trav();
long long ans=;
for(multiset<int>::iterator it=T[].s.begin();it!=T[].s.end();it++)
ans+=(*it);
printf("%I64d\n",ans);
return ;
}
codeforces 965E Trie+multiset的更多相关文章
- CodeForces - 965E Short Code
Discription Arkady's code contains nn variables. Each variable has a unique name consisting of lower ...
- CodeForces 582A【multiset使用样例】
题意: 给一些无序的数字,求解一个矩阵,使得矩阵的每一个元素都是行和列标志数的gcd,输出行标志数. 首先对数字进行排序.复杂度n*log(n^2). 这题的证明有官方的英文题解==在这直接贴英文题解 ...
- Codeforces 965E Short Code 启发式合并 (看题解)
Short Code 我的想法是建出字典树, 然后让后面节点最多的点优先向上移到不能移为止, 然后gg. 正确做法是对于当前的节点如果没有被占, 那么从它的子树中选出一个深度最大的点换到当前位置. 用 ...
- trie树 Codeforces Round #367 D Vasiliy's Multiset
// trie树 Codeforces Round #367 D Vasiliy's Multiset // 题意:给一个集合,初始有0,+表示添加元素,-去除元素,?询问集合里面与x异或最大的值 / ...
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset Trie
题目链接: http://codeforces.com/contest/706/problem/D D. Vasiliy's Multiset time limit per test:4 second ...
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset(可持久化Trie)
D. Vasiliy's Multiset time limit per test 4 seconds memory limit per test 256 megabytes input standa ...
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset trie树
D. Vasiliy's Multiset time limit per test 4 seconds memory limit per test 256 megabytes input standa ...
- codeforces 706D D. Vasiliy's Multiset(trie树)
题目链接: D. Vasiliy's Multiset time limit per test 4 seconds memory limit per test 256 megabytes input ...
- Codeforces #367 (Div. 2) D. Vasiliy's Multiset (trie 树)
http://codeforces.com/group/1EzrFFyOc0/contest/706/problem/D 题目:就是有3种操作 + x向集合里添加 x - x 删除x元素,(保证存在 ...
随机推荐
- [Python3网络爬虫开发实战] 1.8.2-Scrapy的安装
Scrapy是一个十分强大的爬虫框架,依赖的库比较多,至少需要依赖的库有Twisted 14.0.lxml 3.4和pyOpenSSL 0.14.在不同的平台环境下,它所依赖的库也各不相同,所以在安装 ...
- ubuntu14.04 Google Chrome can not be run as root
问题如下图:
- Python之文件处理-批量修改md文档内容
目录 Python之文件处理-批量修改md文档内容 Python之文件处理-批量修改md文档内容 #!/usr/bin/env python # -*- coding:utf-8 -*- import ...
- Quartz--Trigger
TriggerQuartz中的触发器,用来告诉调度程序什么时候触发,即Trigger对象是用来触发Job的 触发器通用属性 JobKey StartTime EndTimeJobKey表示job实例的 ...
- 牛客网NOIP赛前集训营 第6场 T1 最长路
[题解] 先建反向图,然后跑拓扑排序求出最长路. 将所有的点按照最长路从小到大分层,把上一层连向这一层的边按照边权为第一关键字.起点的排名为第二关键字排序. 按照这个顺序更新这一层的答案,按照这一层每 ...
- 笔记——python语言规范
Lint 对你的代码运行pylint 定义: pylint是一个在Python源代码中查找bug的工具. 对于C和C++这样的不那么动态的(译者注: 原文是less dynamic)语言, 这些bug ...
- NormalMap 法线贴图
法线贴图+纹理贴图(细节明显) 纹理贴图 法线贴图 法线贴图 存储法线的一张贴图,归一化的法线的 xyz 的值被映射成为对应的 RGB 值.归一化的法线值为[-1,1],RGB的每一个分量为无符号的8 ...
- spark streaming基于Kafka的开发
spark streaming使用Kafka数据源进行数据处理,本文侧重讲述实践使用. 一.基于receiver的方式 在使用receiver的时候,如果receiver和partition分配不当, ...
- hdu 4871 树的分治+最短路记录路径
/* 题意:给你一些节点和一些边,求最短路径树上是k个节点的最长的路径数. 解:1.求出最短路径树--spfa加记录 2.树上进行操作--树的分治,分别处理子树进行补集等运算 */ #include& ...
- bzoj5105 晨跑 数论lcm
“无体育,不清华”.”每天锻炼一小时,健康工作五十年,幸福生活一辈子”在清华,体育运动绝对是同学们生活中 不可或缺的一部分.为了响应学校的号召,模范好学生王队长决定坚持晨跑.不过由于种种原因,每天都早 ...