---
title: String Compression
tags:
  - Dynamic Programming
id: "1208"
categories:
  - - OI
    - Common Skill
date: 2017-07-17 20:50:36
---

## 题目描述

Ivan wants to write a letter to his friend. The letter is a string $s$ consisting of lowercase Latin letters.

Unfortunately, when Ivan started writing the letter, he realised that it is very long and writing the whole letter may take extremely long time. So he wants to write the compressed version of string s instead of the string itself.

The compressed version of string $s$ is a sequence of strings $c_1, s_1, c_2, s_2, \ldots, c_k, s_k$, where $c_i$ is the decimal representation of number $a_i$ (without any leading zeroes) and $s_i$ is some string consisting of lowercase Latin letters. If Ivan writes string $s_1$ exactly $a_1$ times, then string $s_2$ exactly $a_2$ times, and so on, the result will be string $s$.

The length of a compressed version is $|c_1|+|s_1|+|c_2|+|s_2|+ \cdots +|c_k|+|s_k|$. Among all compressed versions Ivan wants to choose a version such that its length is minimum possible. Help Ivan to determine minimum possible length.

## 题意概述

给定一个字符串$s$，你需要将它压缩。你可以将连续出现$c_i$次的不重复子串$s_i$压缩成长度为$|c_i|+|s_i|$的字符串$c_is_i$（其中$|c_i|$表示十进制下$c_i$的位数）。问最少能压缩成几位。

数据范围：$1 \le |s| \le 8000$。

## 算法分析

令$f_i$表示前$i$个字符最少能压缩成几位。从$f_j$转移到$f_i$时，考虑$s$中第$(j+1)$位到第$i$位的子串是否可以被压缩，如果可以则更新$f_i$。预处理出每两个后缀 LCP 的长度，用于快速判断是否可以压缩。

## 代码

```cpp
#include <iostream>
#include <cstring>
using namespace std;
int len, n[8002], f[8002], dp[8002][8002];
string s;
int main()
{
  cin >> s; len = s.length();
  memset(f, 0x1f, sizeof(f));
  f[0] = 0;
  for (int i = len - 1; i >= 0; --i)
    for (int j = len - 1; j >= 0; --j)
      dp[i][j] = s[i] == s[j] ? dp[i + 1][j + 1] + 1 : 0;
  for (int i = 1; i <= len; ++i) {
    int t = i, s = 0;
    while (t) ++s, t /= 10;
    n[i] = s;
  }
  for (int i = 0; i < len; ++i)
    for (int j = 1; i + j <= len; ++j) {
      int num = 1;
      for (int k = i + j; k <= len; k += j) {
        if (dp[i][k - j] < j) break;
        f[k] = min(f[k], f[i] + n[num] + j);
        ++num;
      }
    }
  cout << f[len] << endl;
  return 0;
}
```
