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. Mac休眠后解决卡死转圈问题

    不知什么时候MacBookPro出现盒盖休眠后Wifi连不上,卡死,转圈问题 在网上搜索解决了下,具体什么原因先不用管了,有时间升级下系统 sudo killall airportd 应该是Mojav ...

  2. Vue-Router路由Vue-CLI脚手架和模块化开发 之 vue-router路由

    vue-router路由:Vue.js官网推出的路由管理器,方便的构建单页应用: 单页应用(SPA)只有一个web页面的应用,用户与应用交互时,动态更新该页面的内容:简单来说,根据不同的url与数据, ...

  3. nginx cookie 会话保持功能

    sticky 会话保持,基于自定义cookie 进行会话保持的方式 安装包下载地址:https://github.com/bymaximus/nginx-sticky-module-ng ./conf ...

  4. 封装一个通用的正则,不再为test和replace所烦恼,eval很棒~

  5. MVC4 中的Model显示设置(含显示Shared/DisplayTemplates和编辑Shared/EditorTemplates)

    转载于: MVC4 中的Model显示设置(含显示Shared/DisplayTemplates和编辑Shared/EditorTemplates) 虽然 [Display(Name="XX ...

  6. id、class等各种选择器总结

    1.  id              选择器       #     class        选择器        .     标签         选择器       标签名     群组   ...

  7. Xml & Tomcat

    文档声明: 简单声明, version : 解析这个xml的时候,使用什么版本的解析器解析 <?xml version="1.0" ?> encoding : 解析xm ...

  8. 2019十大安卓手游折扣平台app排行榜

    2019游戏版号陆续开放,玩家又有许多好游戏可以玩了. 小编就以当前最热门的十个游戏来点评手游折扣平台App排行榜吧! 排名第一的游戏: 少年西游记-新征程 老平台,集成SDK,良心平台,覆盖全网游戏 ...

  9. Codeforces Round #503 (by SIS, Div. 2)

    连接:http://codeforces.com/contest/1020 C.Elections 题型:你们说水题就水题吧...我没有做出来...get到了新的思路,不虚.好像还有用三分做的? KN ...

  10. 如何搜索 git 提交记录

    如何搜索 git 提交记录 git log -p --all -G '可通过正则搜索' --pretty=format:'%ci' # 可跨分支搜索 # -S '通过文本搜索' git branch ...