CodeForces - 965E Short Code
Discription
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.
Input
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.
Output
Print a single integer — the minimum possible total length of new variable names.
Examples
3
codeforces
codehorses
code
6
5
abba
abb
ab
aa
aacada
11
3
telegram
digital
resistance
3
Note
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.
很显然是可以在trie树上贪心的。
每个点x开一个map,存单词结尾在x子树内的点的深度集合。
如果点x没有被单词结尾覆盖,那么就把最深的放到这里。
向上合并的时候启发式合并就好啦。。。。
复杂度 O(N * log ^ 2(N))
#include<bits/stdc++.h>
#include <typeinfo>
#define ll long long
using namespace std;
const int maxn=100005; map<int,int> mmp[maxn];
map<int,int> :: iterator it;
int ch[maxn][26],cnt,n,N,ans,dep[maxn],word[maxn];
char S[maxn]; inline void ins(){
int now=0,c,depth=0;
for(int i=0;i<N;i++){
c=S[i]-'a';
if(!ch[now][c]) ch[now][c]=++cnt;
now=ch[now][c],dep[now]=++depth;
} word[now]++;
} void dfs(int x){
for(int i=0,to;i<26;i++) if(ch[x][i]){
to=ch[x][i],dfs(to); if(mmp[to].size()>mmp[x].size()) swap(mmp[to],mmp[x]); for(it=mmp[to].begin();it!=mmp[to].end();++it) mmp[x][it->first]+=it->second; mmp[to].clear();
} if(word[x]) mmp[x][dep[x]]+=word[x];
else if(x){
it=--mmp[x].lower_bound(1e9); ans-=(it->first)-dep[x]; if(!(--(it->second))) mmp[x].erase(it); mmp[x][dep[x]]++;
}
} int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
fill(S,S+N,0);
scanf("%s",S),N=strlen(S),ans+=N;
ins();
} dfs(0); printf("%d\n",ans);
return 0;
}
CodeForces - 965E Short Code的更多相关文章
- Codeforces 965E Short Code 启发式合并 (看题解)
Short Code 我的想法是建出字典树, 然后让后面节点最多的点优先向上移到不能移为止, 然后gg. 正确做法是对于当前的节点如果没有被占, 那么从它的子树中选出一个深度最大的点换到当前位置. 用 ...
- codeforces 965E Trie+multiset
E. Short Code time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路)
[Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路) 题面 有n个空心物品,每个物品有外部体积\(out_i\)和内部体积\(in_i\),如果\(in_i& ...
- 【Codeforces Round #476 (Div. 2) [Thanks, Telegram!] E】Short Code
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 先建立一棵字典树. 显然,某一些节点上会被打上标记. 问题就转化成求所有标记的深度的和的最小值了. (标记可以上移,但是不能在同一位 ...
- Codeforces 543A Writing Code
http://codeforces.com/problemset/problem/543/A 题目大意:n个人,一共要写m行程序,每个程序员每行出现的bug数为ai,要求整个程序出现的bug数不超过b ...
- CodeForces 543A - Writing Code DP 完全背包
有n个程序,这n个程序运作产生m行代码,但是每个程序产生的BUG总和不能超过b, 给出每个程序产生的代码,每行会产生ai个BUG,问在总BUG不超过b的情况下, 我们有几种选择方法思路:看懂了题意之后 ...
- Codeforces 768B B. Code For 1
参考自:https://www.cnblogs.com/ECJTUACM-873284962/p/6423483.html B. Code For 1 time limit per test:2 se ...
- 【codeforces 768B】Code For 1
[题目链接]:http://codeforces.com/contest/768/problem/B [题意] 一开始给你一个数字n; 让你用这个数字n根据一定的规则生成序列; (如果新生成的序列里面 ...
- 【codeforces 765B】Code obfuscation
[题目链接]:http://codeforces.com/contest/765/problem/B [题意] 让你把每个变量都依次替换成a,b,c,-.d这些字母; 且要按顺序先用a再用b-.c.d ...
随机推荐
- 【NOIP模拟赛】Evensgn 的债务 乱搞
biubiu~~~ 我们发现按照这道题的题意我们把一个个人的前后(欠钱,被欠钱)都缩一下,那么他对其他人没有影响,那么我们就可以依次缩完每个人,而且每个人最后的状态都是要买欠要么被欠,那么我们可以知道 ...
- [poj 3693]后缀数组+出现次数最多的重复子串
题目链接:http://poj.org/problem?id=3693 枚举长度L,看长度为L的子串最多能重复出现几次,首先,能出现1次是肯定的,然后看是否能出现两次及以上.由抽屉原理,这个子串出现次 ...
- POJ2492:A Bug's Life(种类并查集)
A Bug's Life Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 45757 Accepted: 14757 题 ...
- event对象和事件冒泡
<!DOCTYPE HTML><html><head> <meta charset="utf-8"> <title>无标 ...
- fscanf函数的应用
转摘自:http://blog.csdn.net/mxgsgtc/article/details/13005675 以前老是被从文本里读取文件,然后逐个的进行字符解析,感觉非常的慢,自从知道了fsca ...
- JVM 性能排查--查看哪个对象占用内存大
参考:http://blog.csdn.net/chenleixing/article/details/44227327/ 1. 在IE地址栏中输入:http://localhost/test/in ...
- 【EOJ3652】乘法还原(二分图)
题意: 思路:Orz Claris 先找出所有平方项,将与有平方项的数有关的数对暂时忽略,剩下的直接连边就是一张二分图,暴力DFS染色 将有平方项的数两边都加一个,再判字典序即可 我不会判字典序……耽 ...
- 为什么VS没有提供平win64程序编写项
最近在学习C++和MFC编程,突然有个疑问,为什么每次新建项目时,都只有win32 console application,从来没见过win64的选项,于是去网上查了查,下面是我找到的几个答案: 作者 ...
- MongoDB简介以及下载安装
什么是MongoDB ? MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统.运行稳定,性能高 在高负载的情况下,添加更多的节点,可以保证服务器性能. MongoDB 旨在 ...
- [FZU2254]英语考试
在过三个礼拜,YellowStar有一场专业英语考试,因此它必须着手开始复习. 这天,YellowStar准备了n个需要背的单词,每个单词的长度均为m. YellowStar准备采用联想记忆法来背诵这 ...