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 ...
随机推荐
- python操作三大主流数据库(7)python操作mongodb数据库①mongodb的安装和简单使用
python操作mongodb数据库①mongodb的安装和简单使用 参考文档:中文版:http://www.mongoing.com/docs/crud.html英文版:https://docs.m ...
- IIS环境下部署https【转载】
1.首先我们要取走我们的证书,保存在我们本地的电脑里,然后复制到服务器即可. 2.取走后接下来干嘛?当然是打开文件看看里面有些什么啊.我们找到IIS那个压缩包并解压. 3.解析得到pfx文件,也就是我 ...
- eclipse:显示堆内存
如下图 :
- POJ 2115
ax=b (mod n) 该方程有解的充要条件为 gcd(a,n) | b ,即 b% gcd(a,n)==0 令d=gcd(a,n) 有该方程的 最小整数解为 x = e (mod n/d) 其中e ...
- mysql定时任务,每天的零点执行一个存储过程
1 前言 利用navicat工具来写存储过程及定时执行,此文章是按照自身经验总结的,仅作为记录使用. 2 步骤 2.1 新建过程 2.2 在函数体写你需要执行的代码 CREATE DEFINER=`r ...
- js——class基础
js的类?其实还是原型! class Point{ constructor(x, y){ this.x = x; this.y = y; } toString(){ return '(' + this ...
- Confluence 6 访问你的宏正文(body)
请查看 Writing User Macros 页面获得有关如何写用户宏的介绍. 这个页面介绍你可以在用户宏中可以使用的的代码信息. 访问你的宏正文(body) 在你用户宏模板中的 $body 对象可 ...
- 洛谷 P3627 [APIO2009]抢掠计划
这题一看就是缩点,但是缩完点怎么办呢?首先我们把所有的包含酒吧的缩点找出来,打上标记,然后建立一张新图, 每个缩点上的点权就是他所包含的所有点的点权和.但是建图的时候要注意,每一对缩点之间可能有多条边 ...
- usrp使用
首先打开linux 输入uhd_find_divice gqrx
- linux目录详解
网卡的配置文件目录 /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 ...