题目大意:
  给你n个字符串,要求从中选出k个字符串,使得字符串两两lcp之和最大。

思路:
  动态规划。
  首先将所有的字符串排序,求出相邻两个字符串的lcp长度(很显然,对于某一个字符串,和它lcp最长的字符串一定是和它字典序最接近的一个)。
  接下来考虑一种类似于分治的做法。
  首先找出当前区间内最小的lcp。
  很显然,在这个lcp左边的字符串和右边的字符串配对时的lcp一定是这个lcp。
  假如我们在左边取了i个,右边取了j个,这个lcp对答案的贡献是lcp*i*j。
  接下来递归处理左半边的区间和右半边的区间即可。
  考虑如何表示状态。
  不难想到用f[l][r][k]表示在l~r之间取k个字符串。
  每次递归枚举左右区间取的个数。
  总共有n^2k种状态,如果使用记忆化,很显然数组开不下。
  不使用记忆化则会TLE。
  接下来考虑改进这个状态。
  我们递归的时候不需要针对某一个具体的k,而是在l,r这个状态内枚举k。
  显然,对于同一个l,r,k,只会被转移一次。
  而同一组l,r只会被转移一次。
  因此我们只需要在递归的时候存一下l,r,然后就把它废弃掉。
  这就相当于动态开数组。
  这样就同时解决了时间和空间上的问题。

 #include<string>
#include<iostream>
#include<algorithm>
const int inf=0x7fffffff;
const int N=,K=;
std::string s[N];
int n,k,lcp[N];
int f[N<<][K],cnt;
void dp(const int &l,const int &r,const int &id) {
if(l==r) return;
int mid=;
for(register int i=l+;i<=r;i++) {
if(lcp[i]<lcp[mid]) mid=i;
}
const int lid=cnt++,rid=cnt++;
dp(l,mid-,lid);
dp(mid,r,rid);
__builtin_memset(f[id],,sizeof f[id]);
for(register int i=;i<=k;i++) {
if(i>mid-l) break;
for(register int j=;j<=k;j++) {
if(j>r-mid+) break;
if(i+j<=k) f[id][i+j]=std::max(f[id][i+j],f[lid][i]+f[rid][j]+lcp[mid]*i*j);
}
}
cnt-=;
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cin>>n>>k;
for(register int i=;i<n;i++) {
std::cin>>s[i];
}
std::sort(&s[],&s[n]);
lcp[]=inf;
for(register int i=;i<n;i++) {
lcp[i]=std::min(s[i].length(),s[i-].length());
for(register int j=;j<lcp[i];j++) {
if(s[i][j]!=s[i-][j]) {
lcp[i]=j;
}
}
}
dp(,n-,cnt++);
std::cout<<f[][k]<<std::endl;
return ;
}

[CodeForces-178F]Representative Sampling的更多相关文章

  1. [Codeforces178F2]Representative Sampling

    Problem 给定n个字符串Si,任意选出k个字符串Ai,使得其中任意两个字符串lcp之和最大. Solution 建一棵trie树,枚举每一个节点对答案的贡献,树形dp,时间复杂度像是O(N^3) ...

  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. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  6. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  7. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  8. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  9. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

随机推荐

  1. c语言学习笔记.预处理.#ifndef

    #ifndef -> if not define 配合 #endif使用 在h头文件中使用,防止重复包含和编译. 也可以用条件编译来实现. 例如: 编写头文件 test.h 在头文件开头写上两行 ...

  2. Anaconda3的安装和汉化

    下载页面 : https://www.anaconda.com/download 直接下载(Windows) : Anaconda3-5.0.0-Windows-x86_64.exe | Anacon ...

  3. sqlite3在Linux下的安装和使用

    自我补充:ubuntu在线安装sqlite3数据库的方法:  系统平台:ubuntu12.04   在ubuntu里面直接使用命令:sudo apt-get install sqlite3 ,出现: ...

  4. 64_n3

    nodejs-yamlish-0.0.5-9.fc26.noarch.rpm 11-Feb-2017 16:48 11966 nodejs-yargs-3.2.1-6.fc26.noarch.rpm ...

  5. js日期工具

    /** * 日期工具类 */ define(function(require, exports, module) { var constants = require("constants&q ...

  6. 121.Best Time to Buy and Sell Stock---dp

    题目链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ 题目大意:给出一串数组,找到差值最大的差 ...

  7. ECMAScript 6 Promise 对象

    一.Promise的含义 所谓Promise,简单说就是一个容器,里面保存着某个未来才会结束的事件(通常是一个异步操作)的结果.从语法上说,Promise是一个对象,从它可以获取异步操作的消息. 1. ...

  8. Tutorial 2: Requests and Responses

    转载自:http://www.django-rest-framework.org/tutorial/2-requests-and-responses/ Tutorial 2: Requests and ...

  9. java版云笔记(八)之关联映射

    Mybatis关联映射 通过数据库对象之间的关联关系,反映到到实体对象之间的引用. 加载多个表中的关联数据,封装到我们的实体对象中. 当业务对数据库进行关联查询. 关联 <association ...

  10. python 面试

    知识总结 面试(一)