forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA.cpp
More file actions
83 lines (69 loc) · 2.1 KB
/
A.cpp
File metadata and controls
83 lines (69 loc) · 2.1 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#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 DEBUG(X) { cout << #X << " = " << 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))
#define ll long long
#define SZ(x) ((int) (x).size())
using namespace std;
const int MN = 5011;
string s;
short cost[MN][MN];
#define next __next
int f[MN], next[MN];
string t;
void trace(int i) {
if (i == 0) return ;
if (f[i] == i) {
REP(x,i) cout << s[x];
cout << " 1" << '\n';
return ;
}
FOR(j,0,i-1) if (f[i] == f[j] + cost[j][i-1]) {
trace(j);
REP(x,cost[j][i-1])
cout << s[j + x];
int len = i - j;
cout << ' ' << len / cost[j][i-1] << '\n';
break;
}
}
int main() {
ios :: sync_with_stdio(0); cin.tie(0);
freopen("decomp.in", "r", stdin);
freopen("decomp.out", "w", stdout);
while (cin >> s) {
// DEBUG
// REP(i,MN) s += (char) ('A' + rand() % 20);
int ls = s.length();
REP(i,ls) {
t = s.substr(i);
next[0] = -1;
int lt = t.length();
int j = -1;
FOR(i,1,lt-1) {
while (j >= 0 && t[j+1] != t[i]) j = next[j];
if (t[j+1] == t[i]) ++j;
next[i] = j;
}
FOR(j,i,ls-1) {
int len = j - i + 1;
int mat = next[j - i] + 1;
// looks wrong
if (mat + mat >= len && len % (len - mat) == 0) cost[i][j] = len - mat;
else cost[i][j] = len;
}
}
f[0] = 0;
FOR(i,1,ls) {
f[i] = i;
FOR(j,0,i-1)
f[i] = min(f[i], f[j] + cost[j][i-1]);
}
cout << f[ls] << '\n';
trace(ls);
}
}