CodeForces 825F String Compression (KMP 求循环节 + DP)
F. String Compression
memory limit per test: 512 megabytes
input: standard input
output: standard output
Description
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 c1, s1, c2, s2, …, 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 s1 exactly a1 times, then string s2 exactly a2 times, and so on, the result will be string s.
The length of a compressed version is |c1| + |s1| + |c2| + |s2|… |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.
Input
The only line of input contains one string s consisting of lowercase Latin letters (1 ≤ |s| ≤ 8000).
Output
Output one integer number — the minimum possible length of a compressed version of s.
Examples
input
1 | aaaaaaaaaa |
output
1 | 3 |
input
1 | abcab |
output
1 | 6 |
input
1 | cczabababab |
output
1 | 7 |
Solution
思路
KMP 求循环节:如果 $len % (len-next [len] == 0)$, 则字符串由循环节组成,循环节长度为 $len - next [len]$,循环 $len \over {len-next [len]}$ 次;否则字符串并无循环节。
$a [i][j]$ 表示子字符串 $s [i,i+1, ….j]$ 循环节长度 + 循环次数的位数。
$dp [i]$ 表示字符串前 $i$ 个字符压缩后的长度。
$dp[i] = min(dp[i], dp[j]+a[j+1][i])$
AC 代码
1 |
|
# | When | Who | Problem | Lang | Verdict | Time | Memory |
---|---|---|---|---|---|---|---|
41132032 | 2018-08-02 21:00:29 | MoonChasing | F - String Compression | GNU C++ | Accepted | 810 ms | 250900 KB |