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. UWP中MarkupExtension的使用

    Xaml作为一种描述语言,在编程中极大地简化了页面开发的繁琐及时间消耗,这得益于它的多种特性:数据绑定.动画.资源文件等等.标记扩展作为其一个特性,在xaml中有不可替代的作用,今天分析下自定义标记扩 ...

  2. CH 6201 走廊泼水节题解

    题目链接:CH6201 当时在海亮考试的第一题: 心得:其实一个算法是要真正理解这个思路和过程,而并不是单单知道它是用来写什么题的: 思路:n个节点有n-1条边,把这n-1条边按照权值从小到大排序,有 ...

  3. pat1067 在离散数学中置换群思想上可用并查集和递归两种方法求解问题

    1.递归求解  注:叙述时 节点其实就是数字0-N-1 !!!最好用一个数组记录0-N-1每个数字的位置 !!!递归计算一个置换群内部的节点数 分为两种情况 累加M,M即是一个置换群所有数字在正确位置 ...

  4. qt界面操作

    //控制死窗体 int width = this->geometry().width(); int height = this->geometry().height(); this-> ...

  5. idea自个常用工具的总结

    1.直接打开某类:ctrl+shift+t2.注释某类:ctrl+?3. implementation :Ctrl+T4.rename:Alt +Shirft +R5.Show Intention A ...

  6. [IoC容器Unity]第一回:Unity预览

    1.引言 高内聚,低耦合成为一个OO架构设计的一个参考标准.高内聚是一个模块或者一个类中成员跟这个模块或者类的关系尽量高,低耦合是不同模块或者不同类之间关系尽量简单. 拿咱国家举例来说,假如你是中国人 ...

  7. 对java中路径的一些理解

    开始前先贴一下项目结构 public class TestLocation { @Test public void test1(){ String s1 = Objects.requireNonNul ...

  8. 使用Keepalived配置主从热备实现Nginx高可用(HA)

    Keepalived 简要介绍 Keepalived 是一种高性能的服务器高可用或热备解决方案,Keepalived 可以用来防止服务器单点故障的发生,通过配合 Nginx 可以实现 web 前端服务 ...

  9. Unity外包 UE4外包 项目案例平台开通通知

    长年承接Unity外包 UE4外包,大家好,本公司团队已将案例上传至专门的案例官网. 欢迎联系我们索取,谢谢! 有项目外包请联系QQ:372900288 索取案例.

  10. 浅谈现公司的Spring Cloud微服务框架

    目录 说在前面 服务注册与发现 服务网关及熔断 配置中心 消息中心.服务链路追踪 小言 说在前面 本文偏小白,大佬慎入,若有错误或者质疑,欢迎留言提问,谢谢,祝大家新年快乐. spring cloud ...