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;
}
'APS > 프로그래머스' 카테고리의 다른 글
[힙(Heap)] 이중우선순위큐 C++ (0) | 2022.01.09 |
---|---|
[힙(Heap)] 더 맵게 (0) | 2022.01.04 |
[2017 카카오코드 본선] 단체사진 찍기 C++ (0) | 2021.12.31 |
[Summer/Winter Coding(~2018)] 영어 끝말잇기 C++ (0) | 2021.12.30 |
[해시]위장 (0) | 2021.12.18 |