forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path3.cpp
More file actions
29 lines (27 loc) Β· 1.18 KB
/
3.cpp
File metadata and controls
29 lines (27 loc) Β· 1.18 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
#include <bits/stdc++.h>
using namespace std;
int solution(string s) {
int answer = s.size();
// 1κ° λ¨μ(step)λΆν° μμΆ λ¨μλ₯Ό λλ €κ°λ©° νμΈ
for (int step = 1; step < s.size() / 2 + 1; step++) {
string compressed = "";
string prev = s.substr(0, step); // μμμλΆν° stepλ§νΌμ λ¬Έμμ΄ μΆμΆ
int cnt = 1;
// λ¨μ(step) ν¬κΈ°λ§νΌ μ¦κ°μν€λ©° μ΄μ λ¬Έμμ΄κ³Ό λΉκ΅
for (int j = step; j < s.size(); j += step) {
// μ΄μ μνμ λμΌνλ€λ©΄ μμΆ νμ(count) μ¦κ°
if (prev == s.substr(j, step)) cnt += 1;
// λ€λ₯Έ λ¬Έμμ΄μ΄ λμλ€λ©΄(λ μ΄μ μμΆνμ§ λͺ»νλ κ²½μ°λΌλ©΄)
else {
compressed += (cnt >= 2)? to_string(cnt) + prev : prev;
prev = s.substr(j, step); // λ€μ μν μ΄κΈ°ν
cnt = 1;
}
}
// λ¨μμλ λ¬Έμμ΄μ λν΄μ μ²λ¦¬
compressed += (cnt >= 2)? to_string(cnt) + prev : prev;
// λ§λ€μ΄μ§λ μμΆ λ¬Έμμ΄μ΄ κ°μ₯ μ§§μ κ²μ΄ μ λ΅
answer = min(answer, (int)compressed.size());
}
return answer;
}