-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmain.cpp
38 lines (35 loc) · 982 Bytes
/
main.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
36
37
38
#include <bits/stdc++.h>
using namespace std;
int num_len(int x) {
int ret = 0;
while(x) {
x /= 10;
ret ++;
}
return ret;
}
int solution(string s) {
int N = s.size();
int answer = N;
for(int i=1;i * 2 <= N; i++) {
int len = N % i;
vector<pair<string, int>> zip_slice;
vector<string> slice;
for(int j = 0; j < N / i * i; j += i) slice.emplace_back(s.substr(j, i));
zip_slice.emplace_back(slice[0], 1);
for(int j = 1; j < (int)slice.size(); j++) {
if(slice[j] == slice[j - 1]) {
zip_slice.back().second += 1;
continue;
}
zip_slice.emplace_back(slice[j], 1);
}
for(int j = 0; j < (int)zip_slice.size(); j++) {
auto [ S, cnt ] = zip_slice[j];
len += (int)S.size();
if(cnt > 1) len += num_len(cnt);
}
answer = min(answer, len);
}
return answer;
}