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的更多相关文章

  1. [CodeForces-178F]Representative Sampling

    题目大意: 给你n个字符串,要求从中选出k个字符串,使得字符串两两lcp之和最大. 思路: 动态规划. 首先将所有的字符串排序,求出相邻两个字符串的lcp长度(很显然,对于某一个字符串,和它lcp最长 ...

  2. 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 ...

  3. Survey sampling

    Survey sampling \(\bullet\)What is survey sampling?(c.f.census survey)(c.f.:参考,查看,来源于拉丁语) \(\bullet\ ...

  4. 图像抠图算法学习 - Shared Sampling for Real-Time Alpha Matting

    一.序言   陆陆续续的如果累计起来,我估计至少有二十来位左右的朋友加我QQ,向我咨询有关抠图方面的算法,可惜的是,我对这方面之前一直是没有研究过的.除了利用和Photoshop中的魔棒一样的技术或者 ...

  5. 【转载】Recommendations with Thompson Sampling (Part II)

    [原文链接:http://engineering.richrelevance.com/recommendations-thompson-sampling/.] [本文链接:http://www.cnb ...

  6. 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 ...

  7. 算法系列:Reservoir Sampling

    copyright © 1900-2016, NORYES, All Rights Reserved. http://www.cnblogs.com/noryes/ 欢迎转载,请保留此版权声明. -- ...

  8. 随机采样方法整理与讲解(MCMC、Gibbs Sampling等)

    本文是对参考资料中多篇关于sampling的内容进行总结+搬运,方便以后自己翻阅.其实参考资料中的资料写的比我好,大家可以看一下!好东西多分享!PRML的第11章也是sampling,有时间后面写到P ...

  9. 【Moqui业务逻辑翻译系列】Sales Representative Seeks Prospects and Opportunities 销售代表寻找期望合作对象和机会

    h1. Sales Representative Seeks Prospects and Opportunities 销售代表寻找期望合作对象和合作机会 h4. Ideas to incorporat ...

随机推荐

  1. idea maven指定编译参数

    "Settings" > "Build, Execution, Deployment" > "Compiler" > &q ...

  2. Linux 最小系统制作

    Linux 最小系统制作 一.制作工具Busybox 在制作文件系统的时候,我们需要使用“Busybox 工具”,即为附件压缩包“busybox-1.21.1.tar.bz2”.“BusyBox 工具 ...

  3. P3224 [HNOI2012]永无乡

    思路 平衡树+启发式合并 貌似也可以线段树合并 连边就是合并两个Treap,查询就是第k大 使用Treap,好写好调 代码 #include <cstdio> #include <a ...

  4. SpringIOC的概念理解、构造器注入、setter注入、p命名空间注入、IOC容器介绍与比较

    1.IOC概念理解 IOC(Inversion of Control)即“控制反转”,不是什么技术,而是一种设计思想.在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对象 ...

  5. L1范数与L2范数​

    L1范数与L2范数​ ​ L1范数与L2范数在机器学习中,是常用的两个正则项,都可以防止过拟合的现象.L1范数的正则项优化参数具有稀疏特性,可用于特征选择:L2范数正则项优化的参数较小,具有较好的抗干 ...

  6. Vue-admin工作整理(五):守卫导航

    一.作用: 它可以帮我们在路由发生跳转,到导航结束的时间内,做一些相应的逻辑处理,分为:全局守卫,和专项守卫 1.全局收尾: (a).前置守卫:router.beforeEach(to,from,ne ...

  7. hibernate 一对多关系中的孤儿属性

    @OneToMany(targetEntity = BenefitType.class, mappedBy = "sitePerson",cascade = CascadeType ...

  8. Python绘制温度变化曲线

    导入必要的第三方库 from requests import get import matplotlib.pyplot as plt /usr/lib/python3/dist-packages/ma ...

  9. 一个SQL语句的优化(sqlserver)

    最早的写法: WITH T AS ( end as Flag FROM YM WHERE Col_076 BETWEEN '2018-07-25' AND '2018-08-03' AND Col_4 ...

  10. prometheus告警插件-alertmanager

    prometheus本身不支持告警功能,主要通过插件alertmanage来实现告警.AlertManager用于接收Prometheus发送的告警并对于告警进行一系列的处理后发送给指定的用户. pr ...