首页 技术 正文
技术 2022年11月19日
0 收藏 680 点赞 4,883 浏览 3058 个字

题目链接:【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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,028
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,518
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,365
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,146
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,780
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,857