forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB.cpp
More file actions
66 lines (57 loc) · 1.97 KB
/
B.cpp
File metadata and controls
66 lines (57 loc) · 1.97 KB
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
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define DEBUG(x) { cout << #x << " = "; cout << (x) << endl; }
#define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; }
#define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
using namespace std;
const int MN = 511;
string s;
#define equal ____equal
int f[MN][MN], equal[MN][MN];
bool good[MN][MN][MN];
int n;
int getlen(int x) {
int res = 0;
while (x) {
res++;
x /= 10;
}
return res;
}
int main() {
ios :: sync_with_stdio(false);
int test = 0;
while (cin >> s && s != "0") {
n = s.length();
s = " " + s;
memset(equal, 0, sizeof equal);
FORD(i,n,1) FORD(j,n,i+1) {
if (s[i] != s[j]) equal[i][j] = 0;
else equal[i][j] = equal[i+1][j+1] + 1;
}
FORD(i,n,1) FOR(j,i,n) {
FOR(k,i,j) {
int l_ik = k - i + 1;
if (k == j) good[i][j][k] = true;
else if ((j - i + 1) % l_ik) good[i][j][k] = false;
else good[i][j][k] = good[i][j-l_ik][k] && equal[i][j-l_ik+1] >= l_ik;
}
}
FORD(i,n,1) FOR(j,i,n) {
f[i][j] = j - i + 1;
FOR(k,i,j-1) {
f[i][j] = min(f[i][j], f[i][k] + f[k+1][j]);
if (good[i][j][k]) {
if (i == k) f[i][j] = min(f[i][j], f[i][k] + getlen((j - i + 1) / (k - i + 1)));
else f[i][j] = min(f[i][j], 2 + f[i][k] + getlen((j - i + 1) / (k - i + 1)));
}
}
}
cout << "Case " << ++test << ": " << f[1][n] << '\n';
}
return 0;
}