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 j1i1=j2i2j1−i1=j2−i2, j1i1j1≥i1, j2i2j2≥i2, and for every t[0,j1i1]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 = 2;
const int mod[M] = { (int)1e9 + 7, (int)1e9 + 9 };
struct Hash
{
    int a[M];
    Hash(int x = 0)
    {
        for (int i = 0; i < M; i++)
            a[i] = x;
    }
    Hash(const vector<int> &v)
    {
        for (int i = 0; i < M; i++)
            a[i] = v[i];
    }
    Hash operator * (const Hash &x) const
    {
        Hash ret;
        for (int i = 0; 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 = 0; i < M; i++)
        {
            ret.a[i] = a[i] - x.a[i];
            if (ret.a[i] < 0)
                ret.a[i] += mod[i];
        }
        return ret;
    }
    Hash operator + (const Hash &x) const
    {
        Hash ret;
        for (int i = 0; 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 = 0; i < M; i++)
            if (a[i] != x.a[i])
                return false;
        return true;
    }
};
const Hash seed = Hash({ 131, 137 });


const int maxn = 1e5 + 15;
const int maxm = 350;
Hash sum[maxm];
int n, len[maxm], dp[maxm][maxm];
char s[maxn];

Hash Hash_tab(int ln)
{
    Hash ret;
    for(int i = 0; i < ln; i++)
    {
        LL tmp = (LL)(s[i] - 'a' + 1);
        ret = ret * seed + Hash({tmp, tmp});
    }
    return ret;
}

int main ()
{
    scanf("%d", &n);
    int sum_len = n - 1;
    for(int i = 0; i < n; i++)
    {
        scanf("%s", s);
        len[i] = strlen(s);
        sum_len += len[i];
        sum[i] = Hash_tab(len[i]);
    }
    for(int i = n - 2; i >= 0; i--)
        for(int j = n - 1; j >= 0 && j > i; j--)
            dp[i][j] = (len[i] == len[j] && sum[i] == sum[j]) ? 1 + dp[i + 1][j + 1] : 0;
    int ans = sum_len;
    for(int i = 0; i < n; i++)
    {
        int ret = -1;
        for(int j = 0; i + j < n; j++)
        {
            ret += len[i + j];
            int cnt = 1, k = i + j + 1;
            while(k < n)
            {
                if(dp[i][k] >= j + 1)
                {
                    k += j;
                    cnt++;
                }
                k++;
            }
            if(cnt >= 2)
                ans = min(ans, sum_len - ret * cnt);
        }
    }
    printf("%d\n", ans);
    return 0;
}

 

posted @ 2018-07-05 22:59  _Mickey  阅读(637)  评论(0编辑  收藏  举报