[프로그래머스] 문자열 압축
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int solution(string s) {
int answer = s.length();
for (int len = 1; len <= s.length(); ++len) {
string res = "";
int cnt = 1;
int pos = 0;
while (pos + len <= s.length()) {
string now = s.substr(pos, len);
if (pos + (2 * len) > s.length()) {
if (cnt == 1) res += now;
else res += (to_string(cnt) + now);
res += s.substr(pos + len);
break;
}
string next = s.substr(pos + len, len);
if (now == next) cnt++;
else {
if (cnt == 1) res += now;
else res += (to_string(cnt) + now);
cnt = 1;
}
pos += len;
}
if (res.length() < answer) answer = res.length();
}
return answer;
}
int main() {
solution("aabbaccc");
}