[Codeforces178F2]Representative Sampling
Problem
给定n个字符串Si,任意选出k个字符串Ai,使得其中任意两个字符串lcp之和最大。
Solution
建一棵trie树,枚举每一个节点对答案的贡献,树形dp,时间复杂度像是O(N^3)
由于每个点对只在自己LCA的时候枚举到贡献,所以是O(N^2)
Notice
这道题分析时间复杂度十分重要
Code
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define reg register int
#define rep(i, a, b) for (reg i = a; i <= b; i++)
#define per(i, a, b) for (reg i = a; i >= b; i--)
#define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
const int INF = 1e9, N = 2000, M = N * 500;
const double eps = 1e-6, phi = acos(-1);
ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
int S = 0, num = 0, cnt[M + 5], deep[M + 5], head[M + 5], F[N * 2 + 5][N + 5], Size[M + 5], son[M][26];
char st[N + 5];
struct node
{
int vet, next, val;
}edge[2 * M];
void add(int u, int v, int w)
{
edge[++num].vet = v;
edge[num].next = head[u];
edge[num].val = w;
head[u] = num;
}
void dfs(int u, int fa, int last)
{
deep[u] = deep[fa] + 1;
int tot = cnt[u];
rep(i, 0, 25)
if (son[u][i]) tot++;
if (tot != 1 || cnt[u]) add(last, u, deep[u] - deep[last]);
rep(i, 0, 25)
if (son[u][i])
if (tot == 1 && !cnt[u]) dfs(son[u][i], u, last);
else dfs(son[u][i], u, u);
}
int dp(int u, int fa, int len)
{
int now = ++S;
Size[u] = cnt[u];
per(i, cnt[u], 1) F[now][i] = i * (i - 1) / 2 * len;
travel(i, u)
{
int v = edge[i].vet;
if (v == u) continue;
int pre = dp(v, u, edge[i].val);
Size[u] += Size[v];
per(j, Size[u], 1)
rep(k, 1, min(j, Size[v]))
F[now][j] = max(F[now][j], F[now][j - k] + F[pre][k] + len * (j - k) * k + len * k * (k - 1) / 2);
}
return now;
}
int sqz()
{
memset(head, 0, sizeof head);
int n = read(), m = read(), point = 0;
rep(i, 1, n)
{
scanf("%s", st + 1);
int len = strlen(st + 1), now = 0;
rep(j, 1, len)
{
if (!son[now][st[j] - 'a']) son[now][st[j] - 'a'] = ++point;
now = son[now][st[j] - 'a'];
}
cnt[now]++;
}
dfs(0, -1, 0);
dp(0, -1, 0);
printf("%d\n", F[1][m]);
return 0;
}
[Codeforces178F2]Representative Sampling的更多相关文章
- [CodeForces-178F]Representative Sampling
题目大意: 给你n个字符串,要求从中选出k个字符串,使得字符串两两lcp之和最大. 思路: 动态规划. 首先将所有的字符串排序,求出相邻两个字符串的lcp长度(很显然,对于某一个字符串,和它lcp最长 ...
- Simple Random Sampling|representative sample|probability sampling|simple random sampling with replacement| simple random sampling without replacement|Random-Number Tables
1.2 Simple Random Sampling Census, :全部信息 Sampling: 抽样方式: representative sample:有偏向,研究者选择自己觉得有代表性的sam ...
- Survey sampling
Survey sampling \(\bullet\)What is survey sampling?(c.f.census survey)(c.f.:参考,查看,来源于拉丁语) \(\bullet\ ...
- 图像抠图算法学习 - Shared Sampling for Real-Time Alpha Matting
一.序言 陆陆续续的如果累计起来,我估计至少有二十来位左右的朋友加我QQ,向我咨询有关抠图方面的算法,可惜的是,我对这方面之前一直是没有研究过的.除了利用和Photoshop中的魔棒一样的技术或者 ...
- 【转载】Recommendations with Thompson Sampling (Part II)
[原文链接:http://engineering.richrelevance.com/recommendations-thompson-sampling/.] [本文链接:http://www.cnb ...
- PRML读书会第十一章 Sampling Methods(MCMC, Markov Chain Monte Carlo,细致平稳条件,Metropolis-Hastings,Gibbs Sampling,Slice Sampling,Hamiltonian MCMC)
主讲人 网络上的尼采 (新浪微博: @Nietzsche_复杂网络机器学习) 网络上的尼采(813394698) 9:05:00 今天的主要内容:Markov Chain Monte Carlo,M ...
- 算法系列:Reservoir Sampling
copyright © 1900-2016, NORYES, All Rights Reserved. http://www.cnblogs.com/noryes/ 欢迎转载,请保留此版权声明. -- ...
- 随机采样方法整理与讲解(MCMC、Gibbs Sampling等)
本文是对参考资料中多篇关于sampling的内容进行总结+搬运,方便以后自己翻阅.其实参考资料中的资料写的比我好,大家可以看一下!好东西多分享!PRML的第11章也是sampling,有时间后面写到P ...
- 【Moqui业务逻辑翻译系列】Sales Representative Seeks Prospects and Opportunities 销售代表寻找期望合作对象和机会
h1. Sales Representative Seeks Prospects and Opportunities 销售代表寻找期望合作对象和合作机会 h4. Ideas to incorporat ...
随机推荐
- 【做题】CERC2017B. Buffalo Barricades——时间倒流
原文链接 https://www.cnblogs.com/cly-none/p/CERC2017B.html 题意:在一个网格平面上,有\(n\)个点,其中第\(i\)个点在以\((x_i, y_i) ...
- Cordova开箱注意事项
在进行cd 目录切换的时候 一定要 注意大小写 不然 node_modules 下的依赖包 会报错 区分大小写 Cordova 在打包的时候报错 一般是版本或sdk问题 cordova 的版本要和co ...
- Hive 导入 parquet 格式数据(未完,待续)
Hive 导入 parquet 格式数据 Parquet 格式文件,查看Schema Parquet 之mapreduce Hive 导入 parquet 格式数据
- npm版本安装问题
问题一 描述 运行npm install之后,前端页面console控制台报错,invalid props. 排查 1. 排除了代码问题,完全一样的代码,其他人的运行无误. 2.猜想可能是版本号问题, ...
- aop(execution()表达式)
execution(* com.tp.soft.service..*.*(..)) 整个表达式可以分为五个部分: 1.execution(): 表达式主体. 2.第一个*号:表示返回类型,*号表示所有 ...
- Android studio 使用startService报错:IllegalStateException
Android 8.0启动service报错:java.lang.RuntimeException和java.lang.IllegalStateException 错误信息: java.lang.Ru ...
- Cow Cycling 动态规划
1552: Cow Cycling 时间限制(普通/Java):1000MS/10000MS 内存限制:65536KByte总提交: 39 测试通过:20 描述 The ...
- virtual box centos7 common operation
======= network part =======1.设置桥接模式 2.vi /etc/sysconfig/network-scripts/ifcfg-enp0s3BOOTPROTO=stati ...
- day24
## 复习 1.类对象属性的查找顺序: 先找自身再找类1)类的名称空间:直接写在类中2) 对象的名称空间:写在__init__方法中,通过self.属性形成名称空间中的名字3) 类的方法:在类中用@c ...
- Java并发编程的挑战
并发编程的目的是为了让程序运行得更快,但是,并不是线程启动的越多,就能让程序最大限度地并发执行.并发编程时,会面临非常多的挑战,比如上下文切换的问题,死锁的问题,以及受限于各种硬件和软件的资源限制问题 ...