Codeforces 965E Short Code 启发式合并 (看题解)
我的想法是建出字典树, 然后让后面节点最多的点优先向上移到不能移为止, 然后gg。
正确做法是对于当前的节点如果没有被占, 那么从它的子树中选出一个深度最大的点换到当前位置。
用set启发式合并维护一下。
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = 2e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); string s[N];
int depth[N], ch[N][], cnt, n;
LL ans;
bool val[N];
multiset<int> Set[N]; void Insert(const string& s) {
int u = ;
for(int i = ; i < SZ(s); i++) {
if(!ch[u][s[i] - 'a']) {
ch[u][s[i] - 'a'] = ++cnt;
depth[cnt] = depth[u] + ;
}
u = ch[u][s[i] - 'a'];
}
val[u] = true;
} void dfs(int u) {
for(int i = ; i < ; i++) {
int v = ch[u][i];
if(v) {
dfs(ch[u][i]);
if(SZ(Set[u]) < SZ(Set[v])) Set[u].swap(Set[v]);
for(auto& x : Set[v]) Set[u].insert(x);
}
}
if(u) {
if(val[u]) Set[u].insert(depth[u]);
else {
if(SZ(Set[u])) {
int val = *Set[u].rbegin();
Set[u].erase(Set[u].lower_bound(val));
Set[u].insert(depth[u]);
}
}
}
} int main() {
cin >> n;
for(int i = ; i <= n; i++) cin >> s[i];
for(int i = ; i <= n; i++) Insert(s[i]);
dfs();
for(auto& x : Set[]) ans += x;
printf("%lld\n", ans);
return ;
} /*
*/
Codeforces 965E Short Code 启发式合并 (看题解)的更多相关文章
- CodeForces - 965E Short Code
Discription Arkady's code contains nn variables. Each variable has a unique name consisting of lower ...
- CodeForces 958F3 Lightsabers (hard) 启发式合并/分治 多项式 FFT
原文链接http://www.cnblogs.com/zhouzhendong/p/8835443.html 题目传送门 - CodeForces 958F3 题意 有$n$个球,球有$m$种颜色,分 ...
- Codeforces 229E Gifts 概率dp (看题解)
Gifts 感觉题解写的就是坨不知道什么东西.. 看得这个题解. #include<bits/stdc++.h> #define LL long long #define LD long ...
- Codeforces 1155F Delivery Oligopoly dp(看题解)
看别人写的才学会的... 我们考虑刚开始的一个点, 然后我们枚举接上去的一条一条链, dp[mask]表示当前已经加进去点的状态是mask所需的最少边数. 反正就是很麻烦的一道题, 让我自己写我是写不 ...
- Lomsat gelral CodeForces - 600E (树上启发式合并)
You are given a rooted tree with root in vertex 1. Each vertex is coloured in some colour. Let's cal ...
- codeforces 600E. Lomsat gelral 启发式合并
题目链接 给一颗树, 每个节点有初始的颜色值. 1为根节点.定义一个节点的值为, 它的子树中出现最多的颜色的值, 如果有多种颜色出现的次数相同, 那么值为所有颜色的值的和. 每一个叶子节点是一个map ...
- Codeforces 380D Sereja and Cinema (看题解)
Sereja and Cinema 首先我们可以发现除了第一个人, 其他人都会坐在已入坐人的旁边. 难点在于计算方案数.. 我们可以从外往里把确定的人用组合数算上去,然后缩小范围. #include& ...
- Codeforces 442D Adam and Tree dp (看题解)
Adam and Tree 感觉非常巧妙的一题.. 如果对于一个已经建立完成的树, 那么我们可以用dp[ i ]表示染完 i 这棵子树, 并给从fa[ i ] -> i的条边也染色的最少颜色数. ...
- Codeforces 915G Coprime Arrays 莫比乌斯反演 (看题解)
Coprime Arrays 啊,我感觉我更本不会莫比乌斯啊啊啊, 感觉每次都学不会, 我好菜啊. #include<bits/stdc++.h> #define LL long long ...
随机推荐
- GIT入门文档
集中式(SVN): 集中式版本控制系统,版本库是集中存放在中央服务器的,用的都是自己的电脑,所以要先从中央服务器取得最新的版本,然后开始干活,干完活了,再把自己的活推送给中央服务器. 集中式版本控制系 ...
- boost.Asio lib
Documentation for Boost.Asio http://www.boost.org/doc/libs/1_62_0/doc/html/boost_asio.html https://w ...
- UML建模图
UML 2.0规范 迅速成为建立软件系统可视化.规范.文档的标准.统一建模语言(UML) 也被用于非软件系统的建模,并在很多领域,诸如金融,军事,工程方面应用广泛. UML 2 定义了13种基本的图, ...
- 用layui前端框架弹出form表单以及提交
第一步:引用两个文件 第二步:点击删除按钮弹出提示框 /*删除开始*/ $(".del").click(function () { var id = $(this).attr(&q ...
- Struts2配置拦截器
<package name="loginaction" namespace="/" extends="struts-default"& ...
- 24)django-信号
目录 1)django信号简介 2)django内置信号 3)django自定义信号 一:django信号简介 Django中提供了“信号调度”,用于在框架执行操作时解耦. 通俗来讲,就是一些动作发生 ...
- Codeforces Educational Codeforces Round 57 题解
传送门 Div 2的比赛,前四题还有那么多人过,应该是SB题,就不讲了. 这场比赛一堆计数题,很舒服.(虽然我没打) E. The Top Scorer 其实这题也不难,不知道为什么这么少人过. 考虑 ...
- 8大排序之Python实现 冒泡排序优化
1.冒泡排序(从大到小):交换发生在内部循环 稳定的排序 冒泡排序的平均时间复杂度是O(n2),最好的时间复杂度是O(n),最坏的时间复杂度是O(n2),空间复杂度为O(1) 冒泡排序的优化在于did ...
- Ubuntu16.04安装JDK8与Tomcat7
本篇教程在示例步骤中使用了以下版本的软件.操作时,请您以实际软件版本为准. 操作系统:Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-105-generic x86_64) ...
- Confluence 6 的系统配置信息的示例
awt.toolkit sun.awt.X11.XToolkit file.encoding.pkg sun.io java.specification.version 1.8 sun.cpu.isa ...