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 ...
随机推荐
- 【BZOJ 2503】相框 图论+讨论
这道题目就是考验了一下图论基本知识与对可爱的代码实现的应对能力. 我们先分析题干信息.我们要形成相框,那么所有的点的度为2(参与的点),那么所有度大于2的点都需要熔断,而且一次完成所有关于这个点的熔断 ...
- bzoj3196 [TYVJ1730]二逼平衡树 树套树 线段树套替罪羊树
人傻自带大常数 二分的可行性证明: 贴近他的正确答案不会被当作次优解删掉,因为,若二分在他右边发生,那么二分一定会把左边作为优解,左边同理,所以他一定是被扣掉的所以最后一个小于等于一定是正确答案 #i ...
- (一)STM32固件库详解(转载)
本篇博文是转载自emouse,因为不能直接转载,所以是复制过来再发布的. emouse原创文章,转载请注明出处http://www.cnblogs.com/emouse/ 1.1 基于标准外设库的 ...
- POJ2253:Frogger(改造Dijkstra)
Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 64864 Accepted: 20127 题目链接:ht ...
- Could not resolve com.android.support:multidex:1.0.2
http://blog.csdn.net/goodlixueyong/article/details/50992835
- jquery的ajax实现方式
在JQuery中,AJAX有三种实现方式:$.ajax() , $.post , $.get(). 首先我们看$.get(): .代码如下: $.get("test.jsp", { ...
- MySQL实现分页查询
imit 基本实现方式 一般情况下,客户端通过传递 pageNo(页码).pageSize(每页条数)两个参数去分页查询数据库中的数据,在数据量较小(元组百/千级)时使用 MySQL自带的 limit ...
- codefoeces problem 671D——贪心+启发式合并+平衡树
D. Roads in Yusland Mayor of Yusland just won the lottery and decided to spent money on something go ...
- codeforces739C - Skills &&金中市队儿童节常数赛
http://codeforces.com/problemset/problem/739/C 先上链接 这道题 对于蒟蒻的我来说还是很有难度的 调了很久 对于我的代码 mx2是答案 mx1代表单调 m ...
- bzoj 1951 lucas crt 费马小定理
首先假设输入的是n,m 我们就是要求m^(Σ(c(n,i) i|n)) mod p 那么根据费马小定理,上式等于 m^(Σ(c(n,i) i|n) mod (p-1)) mod p 那么问题的关键就 ...