#include <iostream>
#include <string>
using namespace std;
int main() {
// input
string sentence;
cin >> sentence;
int sentence_len = sentence.length(), cnt = 1;
char curr_char = sentence[0];
int compressed_length = 0;
for (int i = 1; i < sentence_len; i++) {
if (sentence[i] != curr_char) {
compressed_length += 1 + to_string(cnt).length();
curr_char = sentence[i];
cnt = 1;
} else {
cnt++;
}
}
compressed_length += 1 + to_string(cnt).length();
cout << compressed_length << endl;
cnt = 1;
curr_char = sentence[0];
for (int i = 0; i < sentence_len - 1; i++) {
if (sentence[i] != sentence[i+1]) {
cout << curr_char << cnt;
curr_char = sentence[i+1];
cnt = 1;
} else{
cnt ++;
}
}
cout << curr_char << cnt;
return 0;
}
코드트리 | 코딩테스트 준비를 위한 알고리즘 정석
국가대표가 만든 코딩 공부의 가이드북 코딩 왕초보부터 꿈의 직장 코테 합격까지, 국가대표가 엄선한 커리큘럼으로 준비해보세요.
www.codetree.ai
'Algorithm(CodeTree, C++) > 문자열' 카테고리의 다른 글
[코드트리] 문자열 추가하기 C++ (0) | 2024.06.15 |
---|---|
[코드트리] 짝수 번째만 거꾸로 출력 C++ (0) | 2024.06.10 |
[코드트리] 문자열 범위 출력하기 2 C++ (0) | 2024.06.09 |
[코드트리] 문자열에서 문자 출력 C++ (0) | 2024.06.09 |
[코드트리] 특정 문자로 끝나는 문자열 C++ (0) | 2024.06.09 |