forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
35 lines (33 loc) · 1008 Bytes
/
Solution.cpp
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
class Solution {
public:
string encode(string s) {
int n = s.size();
vector<vector<string>> f(n, vector<string>(n));
auto g = [&](int i, int j) {
string t = s.substr(i, j - i + 1);
if (t.size() < 5) {
return t;
}
int k = (t + t).find(t, 1);
if (k < t.size()) {
int cnt = t.size() / k;
return to_string(cnt) + "[" + f[i][i + k - 1] + "]";
}
return t;
};
for (int i = n - 1; ~i; --i) {
for (int j = i; j < n; ++j) {
f[i][j] = g(i, j);
if (j - i + 1 > 4) {
for (int k = i; k < j; ++k) {
string t = f[i][k] + f[k + 1][j];
if (t.size() < f[i][j].size()) {
f[i][j] = t;
}
}
}
}
}
return f[0][n - 1];
}
};