1. 程式人生 > >CF 494 F. Abbreviation(動態規劃)

CF 494 F. Abbreviation(動態規劃)

一個 ash 統計 code equal 公共前綴 cto amp uppercase

題目鏈接:【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, j1i1j1≥i1,

j2i2j2≥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 = 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;
}

CF 494 F. Abbreviation(動態規劃)