1351 dp in the range of ultraviolet
Ultraviolet A 1351-String Compression
Ask how short a string can be after compression. The compression rule is that if there is a continuous repeated substring (for example, abcabcabcad), it can be compressed into the number of duplicates + (repeated substring) (in the preceding example, the answer is 3 (abc) ad ). The length of the compressed string must contain digits and parentheses.
Range dp, dp stores the minimum length that can be compressed in this range.
Each interval can be divided into two subintervals, or the interval can be compressed. The Preprocessing result shows that each interval can contain up to several substrings: c [I] [j].
Dp [I] [j] = min {dp [I] [k] + dp [k] [j], digits (s) + dp [I] [l] + 2 };
L = (j-I + 1)/c [I] [j];
S = c [I] [j];
The Preprocessing of the c array can be completed by O (n ^ 3. Enumerate the length and start position of the duplicate substring, and then find it from the start position. For each string found, c [I] [j] + 1. For details, see the code.
#include
using namespace std;const int INF = 999999999;int len;char s[205];int _c[205][205];int dp[205][205];int _degits(int num) {int cnt = 0;while (num) {cnt++;num /= 10;}return cnt;}void _solve() {for(int i=0; i
0; T--) {scanf (%s, s);len = strlen(s);_solve();for (int i=0; i
=0; i--) {dp[i][j] = INF;for (int k=i; k