-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLv1_문자열압축.cpp
54 lines (44 loc) · 855 Bytes
/
Lv1_문자열압축.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int solution(string s) {
int slen = s.length();
int answer = slen;
for (int i = 1; i <= slen; ++i) {
int ans = i;
string start = s.substr(0, i);
int index = i; // 시작 인덱스
int count = 1;
while (index < slen) {
int npos = i;
if (index + npos >= slen)
npos = slen - index;
string ns = s.substr(index, npos);
if (start == ns) {
++count;
}
else {
if (count != 1) {
int num = to_string(count).length();
ans += num;
}
ans += npos;
count = 1;
}
start = ns;
index += npos;
}
if (count != 1) {
int num = to_string(count).length();
ans += num;
}
answer = min(answer, ans);
}
return answer;
}
int main() {
cout << solution("aabbaccc");
return 0;
}