APS/프로그래머스
[2020 KAKAO BLIND RECRUITMENT] 문자열 압축 C++
문래동까마귀
2022. 1. 2. 00:12
https://programmers.co.kr/learn/courses/30/lessons/60057
코딩테스트 연습 - 문자열 압축
데이터 처리 전문가가 되고 싶은 "어피치"는 문자열을 압축하는 방법에 대해 공부를 하고 있습니다. 최근에 대량의 데이터 처리를 위한 간단한 비손실 압축 방법에 대해 공부를 하고 있는데, 문
programmers.co.kr
#include <string>
#include <vector>
#include <iostream>
using namespace std;
int solution(string s) {
int answer = 0;
int MIN = 21e8;
if (s.size() == 1)
MIN = 1;
for (int i = 1; i <= s.size()/2; i++) {
string result;
int st=0;
int cnt = 1;
while (1) {
string st1 = s.substr(st, i);
if (st + i > s.size()) {
result += st1;
break;
}
string st2 = s.substr(st + i, i);
if (st1 == st2)
cnt++;
else {
if (cnt != 1)
result += to_string(cnt);
result += st1;
cnt = 1;
}
st += i;
if (st >= s.size())
break;
}
if (result.size() < MIN)
MIN = result.size();
}
answer = MIN;
return answer;
}
int main() {
//TC5번
cout << solution("a") << endl;// 1
cout << solution("aaaaaaaaaaaaaaabbbbbbbbbbc") << endl;// 7
cout << solution("aabbaccc") << endl;// 7
cout << solution("ababcdcdababcdcd") << endl;// 9
cout << solution("abcabcdede") << endl;// 8
cout << solution("abcabcabcabcdededededede") << endl;// 14
cout << solution("xababcdcdababcdcd") << endl;// 17
cout << solution("acdhdh") << endl;// 5
cout << solution("xztjabcdabcd") << endl;// 9
return 0;
}