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. Hadoop-Impala学习笔记之SQL参考

    参考:Apache Impala Guide--Impala SQL Language Reference. Impala使用和Hive一样的元数据存储,Impala可以访问使用原生Impala CR ...

  2. node全局安装说明(create-react-app、)

    1.使用 create-react-app 快速构建 React 开发环境 国内使用 npm 速度很慢,你可以使用淘宝定制的 cnpm (gzip 压缩支持) 命令行工具代替默认的 npm: $ np ...

  3. 解析JavaScript中的sort()排序方法以及原理

    Array.sort()方法将数组中的元素进行排序,返回排序后的数组,默认是按照升序排序的.sort方法会调用数组中每一项的toString()方法,然后按照ascii编码进行排序,如果数组含有und ...

  4. 转载redis持久化的几种方式

    redis持久化的几种方式 1.前言 Redis是一种高级key-value数据库.它跟memcached类似,不过数据可以持久化,而且支持的数据类型很丰富.有字符串,链表,集 合和有序集合.支持在服 ...

  5. 解决win10 蓝牙设备只能配对无法连接 ,并且删除设备无效的问题

    系统环境: win10家庭版 dell本 问题描述:蓝牙设备(比如蓝牙键盘,蓝牙音箱)出现无法连接的情况,打算删除已配对的设备,再重新配对连接.但删除设备后重启蓝牙,那些原本被删除的设备又自动配对上, ...

  6. python实现简单二分查找

    #!/usr/bin/pythondef binary_search(list, item): low = 0 high = len(list)-1 while low <= high: mid ...

  7. php接口实现拖拽排序功能

    列表拖拽排序是一个很常见的功能,但是后端接口如何处理却是一个令人纠结的问题 如何实现才能达到效率最高呢 先分析一个场景,假如有一个页面有十条数据,所谓的拖拽就是在这十条数据来来回回的拖,但是每次拖动都 ...

  8. OpenGL.教程

    5.第五课:带纹理的立方体.html(http://www.opengl-tutorial.org/cn/beginners-tutorials/tutorial-5-a-textured-cube/ ...

  9. git pull 撤销误操作

    本来想把github上的release合并到本地的release分支上,由于没有查看当前分支,直接运用git pull origin v2.8.1,结果将release合并到了v2.8.1分支中. 解 ...

  10. SPI通信的基础知识

    1 SPI物理层 SPI通信设备之间常用物理连接方式如下图  SPI通讯使用3条总线及片选线,3条总线分别为SCK.MOSI.MISO,片选线为CS.  CS:从设备选择信号线,常称为片选信号线,也称 ...