CodeForces 825F String Compression (KMP 求循环节 + DP)

F. String Compression

time limit per test: 2 seconds
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <utility>
#include <algorithm>
#define MAXN 8005
#define INF 0x3f3f3f3f
#define DEBUG
#define DataIn
typedef long long LL;

using namespace std;
int next[MAXN];
char s[MAXN];
int len;
int dp[MAXN];
int a[MAXN][MAXN];

void prekmp(char x[])
{
int i = 0, j = -1;
next[0] = -1;
while(i < len)
{
while(j!=-1 && x[i]!=x[j])
j = next[j];
next[++i] = ++j;
}
}

int digitcnt(int k)
{
int cnt = 0;
while(k)
{
k /= 10;
cnt++;
}
return cnt;
}

void init()
{
for(int i = 0; i < len; i++)
{
prekmp(s + i);
for(int j = i; j < len; j++)
{
if(j == i)
{
a[i][j] = 2;
continue;
}

int t1 = j - i + 1, t2 = t1 - next[t1];
a[i][j] = t1 % t2 == 0 ? t2 + digitcnt(t1/t2) : t1 + 1;
}
}
}

int main()
{
scanf("%s", s);
len = strlen(s);
fill(dp, dp+len, INF);
init();
dp[0] = 2;
for(int i = 1; i < len;i ++)
{
dp[i] = a[0][i];
for(int j = 0; j < i; j ++)
dp[i] = min(dp[i],dp[j] + a[j + 1][i]);
}
printf("%d\n",dp[len-1]);

return 0;
}

mark

# 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