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 ...
随机推荐
- Cassandra docker 使用记录
环境介绍: docker 安装 cassandra 3.11.1 , 然后进入docker 的终端,输入 > cqlsh , 即可使用Cassandra了,详细介绍如下: 查看表空间descri ...
- ubuntu 精简配置
是这样的情况,在windows 7的vm虚拟机上装了Ubuntu 12.04 Desktop,主要是想用它的gui, 所以把不要的给删了. sudo apt-get -y --auto-remove ...
- CF 189A Cut Ribbon
#include<bits/stdc++.h> using namespace std; const int maxn = 4000 + 131; int n, a, b, c; int ...
- Sql语句之并(UNION)、交(INTERSECT)、差(minus)、除去(EXCEPT)
UNION 查询选修了180101号或180102号课程或二者都选修了的学生学号.课程号和成绩. (SELECT 学号, 课程号, 成绩 FROM 学习 WHERE 课程号='180101' ...
- 连接mysql(建表和删表)
from sqlalchemy.ext.declarative import declarative_base##拿到父类from sqlalchemy import Column##拿到字段from ...
- 《JavaWeb从入门到精通》(明日科技,清华大学出版社)
<JavaWeb从入门到精通>(明日科技,清华大学出版社)
- vuex action 与mutations 的区别
面试没说清楚.这个太丢人回来整理下: 事实上在 vuex 里面 actions 只是一个架构性的概念,并不是必须的,说到底只是一个函数,你在里面想干嘛都可以,只要最后触发 mutation 就行.异步 ...
- J2SE基础小结
1. 九种基本数据类型的大小,以及他们的封装类. 类型 基本类型 大小(字节) 默认值 封装类 整数型 byte 1 (byte)0 Byte short 2 (short)0 Short int 4 ...
- Metasploit (二)
1.测试一台搭建的主机 msf > db_nmap -n -A 10.140.110.16[*] Nmap: Starting Nmap 7.60 ( https://nmap.org ) at ...
- 课外知识----base64加密
每3个字符产生4位的base64字符,不足3个字符,将用“=”补齐至4位base64字符 例如 00---> MDA= 000--->MDAw base64加密特点 加密后的字符数是4的 ...