CF 494 F. Abbreviation(动态规划)
题目链接:【http://codeforces.com/contest/1003/problem/F】
题意:给出一个n字符串,这些字符串按顺序组成一个文本,字符串之间用空格隔开,文本的大小是字母+空格的个数。在这个文本中找k(k>=2)个区间,使得这k个区间完全相同,字符串不能分开,然后把每段的字符串变成单个的字符,并去掉中间的空格。可能有多种方案,求文本的最小长度。【表达能力有限,望理解,具体可以看题目】
You are given a text consisting of nn space-separated words. There is exactly one space character between any pair of adjacent words. There are no spaces before the first word and no spaces after the last word. The length of text is the number of letters and spaces in it. wiwi is the ii-th word of text. All words consist only of lowercase Latin letters.
Let's denote a segment of words w[i..j]w[i..j] as a sequence of words wi,wi+1,…,wjwi,wi+1,…,wj. Two segments of words w[i1..j1]w[i1..j1] and w[i2..j2]w[i2..j2] are considered equal if j1−i1=j2−i2j1−i1=j2−i2, j1≥i1j1≥i1, j2≥i2j2≥i2, and for every t∈[0,j1−i1]t∈[0,j1−i1] wi1+t=wi2+twi1+t=wi2+t. For example, for the text "to be or not to be" the segments w[1..2]w[1..2] and w[5..6]w[5..6] are equal, they correspond to the words "to be".
An abbreviation is a replacement of some segments of words with their first uppercase letters. In order to perform an abbreviation, you have to choose at least two non-intersecting equal segments of words, and replace each chosen segment with the string consisting of first letters of the words in the segment (written in uppercase). For example, for the text "a ab a a b ab a a b c" you can replace segments of words w[2..4]w[2..4] and w[6..8]w[6..8] with an abbreviation "AAA" and obtain the text "a AAA b AAA b c", or you can replace segments of words w[2..5]w[2..5] and w[6..9]w[6..9] with an abbreviation "AAAB" and obtain the text "a AAAB AAAB c".
What is the minimum length of the text after at most one abbreviation?
题解:
dp[i][j]表示从第i个字符出开始的串和从第j个字符串开始的串的有多少个公共前缀字符串。因为n不是很大,所以可以暴力枚举。从前到后枚举,从第i个位置开始,长度为x(x个字符串)的串,有几个重复的,若重复的个数大于等于二则统计答案。实现的时候,可以用string暴力比较,也可以用HASH的方法,把字符串HASH成一个数字,这道题HASH卡的比较严格,我用双值HASH才跑过全部数据。具体看代码。(思路是看了某个大神的代码才理解到的,共同学习)。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int M = ;
const int mod[M] = { (int)1e9 + , (int)1e9 + };
struct Hash
{
int a[M];
Hash(int x = )
{
for (int i = ; i < M; i++)
a[i] = x;
}
Hash(const vector<int> &v)
{
for (int i = ; i < M; i++)
a[i] = v[i];
}
Hash operator * (const Hash &x) const
{
Hash ret;
for (int i = ; i < M; i++)
ret.a[i] = (LL)a[i] * x.a[i] % mod[i];
return ret;
}
Hash operator - (const Hash &x) const
{
Hash ret;
for (int i = ; i < M; i++)
{
ret.a[i] = a[i] - x.a[i];
if (ret.a[i] < )
ret.a[i] += mod[i];
}
return ret;
}
Hash operator + (const Hash &x) const
{
Hash ret;
for (int i = ; i < M; i++)
{
ret.a[i] = a[i] + x.a[i];
if (ret.a[i] >= mod[i])
ret.a[i] -= mod[i];
}
return ret;
}
bool operator == (const Hash &x) const
{
for (int i = ; i < M; i++)
if (a[i] != x.a[i])
return false;
return true;
}
};
const Hash seed = Hash({ , }); const int maxn = 1e5 + ;
const int maxm = ;
Hash sum[maxm];
int n, len[maxm], dp[maxm][maxm];
char s[maxn]; Hash Hash_tab(int ln)
{
Hash ret;
for(int i = ; i < ln; i++)
{
LL tmp = (LL)(s[i] - 'a' + );
ret = ret * seed + Hash({tmp, tmp});
}
return ret;
} int main ()
{
scanf("%d", &n);
int sum_len = n - ;
for(int i = ; i < n; i++)
{
scanf("%s", s);
len[i] = strlen(s);
sum_len += len[i];
sum[i] = Hash_tab(len[i]);
}
for(int i = n - ; i >= ; i--)
for(int j = n - ; j >= && j > i; j--)
dp[i][j] = (len[i] == len[j] && sum[i] == sum[j]) ? + dp[i + ][j + ] : ;
int ans = sum_len;
for(int i = ; i < n; i++)
{
int ret = -;
for(int j = ; i + j < n; j++)
{
ret += len[i + j];
int cnt = , k = i + j + ;
while(k < n)
{
if(dp[i][k] >= j + )
{
k += j;
cnt++;
}
k++;
}
if(cnt >= )
ans = min(ans, sum_len - ret * cnt);
}
}
printf("%d\n", ans);
return ;
}
CF 494 F. Abbreviation(动态规划)的更多相关文章
- CF 633 F. The Chocolate Spree 树形dp
题目链接 CF 633 F. The Chocolate Spree 题解 维护子数答案 子数直径 子数最远点 单子数最长直径 (最长的 最远点+一条链) 讨论转移 代码 #include<ve ...
- CF #271 F Ant colony 树
题目链接:http://codeforces.com/contest/474/problem/F 一个数组,每一次询问一个区间中有多少个数字可以整除其他所有区间内的数字. 能够整除其他所有数字的数一定 ...
- Leetcode 494 Target Sum 动态规划 背包+滚动数据
这是一道水题,作为没有货的水货楼主如是说. 题意:已知一个数组nums {a1,a2,a3,.....,an}(其中0<ai <=1000(1<=k<=n, n<=20) ...
- CF 1138 F. Cooperative Game
F. Cooperative Game 链接 题意: 有10个玩家,开始所有玩家在home处,每次可以让一些玩家沿着边前进一步,要求在3(t+c)步以内,到达终点. 分析: 很有意思的一道题.我们构造 ...
- CF 1041 F. Ray in the tube
F. Ray in the tube 链接 题意: 有两条平行于x轴的直线A,B,每条直线上的某些位置有传感器.你需要确定A,B轴上任意两个整点位置$x_a$,$x_b$,使得一条光线沿$x_a→x_ ...
- 【Cf #502 F】The Neutral Zone
本题把$log$化简之后求得就是每个质数$f$前的系数,求系数并不难,难点在于求出所有的质数. 由于空间限制相当苛刻,$3e8$的$bitset$的内存超限,我们考虑所有的除了$2$和$3$以外的质数 ...
- CF 868 F. Yet Another Minimization Problem
F. Yet Another Minimization Problem http://codeforces.com/contest/868/problem/F 题意: 给定一个长度为n的序列.你需要将 ...
- CF 1051 F. The Shortest Statement
F. The Shortest Statement http://codeforces.com/contest/1051/problem/F 题意: n个点,m条边的无向图,每次询问两点之间的最短路. ...
- CF 1042 F. Leaf Sets
F. Leaf Sets http://codeforces.com/contest/1042/problem/F 题意: 将所有的叶子节点分配到尽量少的集合,一个可行的集合中两两叶子节点的距离< ...
随机推荐
- node爬虫进阶版
手写了一个方便爬虫的小库: const url = require('url') const glib = require('zlib') //默认头部 const _default_headers ...
- ASP.NET MVC学习(五)之MVC原理解析
ASP.NET MVC 请求生命周期 生命周期步骤概览 当我们对ASP.NET MVC网站发出一个请求的时候,会发生5个主要步骤: 步骤1:创建RouteTable 当ASP.NET应用程序第一次启动 ...
- html5 canvas缩放变换
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 【洛谷P3884 [JLOI2009]】二叉树问题
题目描述 如下图所示的一棵二叉树的深度.宽度及结点间距离分别为: 深度:4 宽度:4(同一层最多结点个数) 结点间距离: ⑧→⑥为8 (3×2+2=8) ⑥→⑦为3 (1×2+1=3) 注:结点间距离 ...
- Chrome插件之ModHeader
一.ModHeader是什么 ModHeader顾名思义就是让我们可以自定义HTTP请求头或者是重写响应头,包括新增请求头/响应头或者覆盖Chrome浏览器设置的请求头的默认值,同时还可以根据URL ...
- 采用jacob实现word转pdf
网络上已经有很多这方面的内容,在用之前也是参考了好多别人的文章,下面记录下我自己的整合过程.整个过程都比较简单: 开发环境:win8 64位系统,在2008下面部署也是一样的. 文档要求jdk的版本要 ...
- linux 查看有哪些service
一.利用进程来查看命令里 ps -aux | grep xxx 是查看某个进程或者服务是否存在.二.利用chkconfig配置工具chkconfig --list 可以列出所有的服务在各个runlev ...
- sync 解释
sync命令用于强制被改变的内容立刻写入磁盘,更新超块信息. 在Linux/Unix系统中,在文件或数据处理过程中一般先放到内存缓冲区中,等到适当的时候再写入磁盘,以提高系统的运行效率.sync命令则 ...
- 012_iTerm2 快捷键大全
标签 新建标签:command + t 关闭标签:command + w 切换标签:command + 数字 command + 左右方向键 切换全屏:command + enter 查找:comma ...
- Gentoo rc-update service ‘net.eth0′ does not exist
最近迷上了Gentoo,并相信以后也会把更多的精力放在Gentoo上,不过Gentoo的安装的过程的确让很多人却步. 本文只提到添加net.eth0到默认的运行级别时一个很小的报错解决. # nano ...