题目链接:【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(动态规划)的更多相关文章

  1. CF 633 F. The Chocolate Spree 树形dp

    题目链接 CF 633 F. The Chocolate Spree 题解 维护子数答案 子数直径 子数最远点 单子数最长直径 (最长的 最远点+一条链) 讨论转移 代码 #include<ve ...

  2. CF #271 F Ant colony 树

    题目链接:http://codeforces.com/contest/474/problem/F 一个数组,每一次询问一个区间中有多少个数字可以整除其他所有区间内的数字. 能够整除其他所有数字的数一定 ...

  3. Leetcode 494 Target Sum 动态规划 背包+滚动数据

    这是一道水题,作为没有货的水货楼主如是说. 题意:已知一个数组nums {a1,a2,a3,.....,an}(其中0<ai <=1000(1<=k<=n, n<=20) ...

  4. CF 1138 F. Cooperative Game

    F. Cooperative Game 链接 题意: 有10个玩家,开始所有玩家在home处,每次可以让一些玩家沿着边前进一步,要求在3(t+c)步以内,到达终点. 分析: 很有意思的一道题.我们构造 ...

  5. CF 1041 F. Ray in the tube

    F. Ray in the tube 链接 题意: 有两条平行于x轴的直线A,B,每条直线上的某些位置有传感器.你需要确定A,B轴上任意两个整点位置$x_a$,$x_b$,使得一条光线沿$x_a→x_ ...

  6. 【Cf #502 F】The Neutral Zone

    本题把$log$化简之后求得就是每个质数$f$前的系数,求系数并不难,难点在于求出所有的质数. 由于空间限制相当苛刻,$3e8$的$bitset$的内存超限,我们考虑所有的除了$2$和$3$以外的质数 ...

  7. CF 868 F. Yet Another Minimization Problem

    F. Yet Another Minimization Problem http://codeforces.com/contest/868/problem/F 题意: 给定一个长度为n的序列.你需要将 ...

  8. CF 1051 F. The Shortest Statement

    F. The Shortest Statement http://codeforces.com/contest/1051/problem/F 题意: n个点,m条边的无向图,每次询问两点之间的最短路. ...

  9. CF 1042 F. Leaf Sets

    F. Leaf Sets http://codeforces.com/contest/1042/problem/F 题意: 将所有的叶子节点分配到尽量少的集合,一个可行的集合中两两叶子节点的距离< ...

随机推荐

  1. WinFrom弹出输入框

    代码上面要引用 using Microsoft.VisualBasic; 还不够,在解决方案的引用那里,也要添加引用 如此,便可打出输入框了: ,); 5个参数分别的意思: 提示信息 标题 如果用户没 ...

  2. 基于 Express 搭建一个node项目 - 起步

    一,如何基于 Express 搭建一个node项目 什么是Express 借用官方的介绍,Express是一个基于Node.js平台的极简.灵活的web应用开发框架,它提供了一系列强大的特性,帮助你创 ...

  3. ActiveMQ基础教程JMS概述

    什么是JMS JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送消息 ...

  4. HDU 4315 阶梯博弈变形

    n个棋子,其中第k个是红色的,每个棋子只能往上爬,而且不能越过.重叠其他棋子,谁将红色棋子移到顶部谁赢. 由于只能往上爬,所以很像阶梯博弈.这题有2个限制,棋子不能重叠,有红棋存在 首先不考虑红色棋, ...

  5. html5 canvas贝塞尔曲线篇(上)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. jQuery中Animate进阶用法(二)

    Step Type: Function( Number now, Tween tween )每个动画元素的每个动画属性将调用的函数.这个函数为修改Tween 对象提供了一个机会来改变设置中得属性值. ...

  7. shell 判断文件出现次数

    判断 file 文件中 第一个变量 出现次数 awk '{print $1}' file |sort |uniq -c|sort -k1r

  8. HDU 1867 A + B for you again 字符匹配

    解题报告:给你两个字符串,让你连接起来,没有前后顺序,要求是长度最短优先,其次是字典序最小.这题我用的是KMP,做两次匹配,分别把第一次跟第二次输入的字符串放前面,然后比较两次得到的字符窜的长度和字典 ...

  9. 第12月第8天 Retrofit.builder

    1. retrofit = new Retrofit.Builder() .client(okHttpClient) .addConverterFactory(GsonConverterFactory ...

  10. Zookeeper命名服务——生成分布式有序且唯一id

    生成分布式有序且唯一id的方法有很多种,使用zookeeper是比较简单的一种方法,只是生成的速度不高,这里只是一个借助zk的版本号生成分布式唯一且有序id的例子. ZkIdGenerator.jav ...