킹솔이
[프로그래머스/C++] 2020 KAKAO BLIND RECRUITMENT 문자열 압축 본문
#include<iostream>
#include <string>
#include <vector>
using namespace std;
int solution(string s) {
int answer = 1000;
//압축할 문자열갯수는 s.size()/2가 최대
for (int i = 1; i <= s.size(); i++) {
string str = "";
vector<string> vec;
int j = 0;
while (j < s.size()) {
vec.push_back(s.substr(j, i));
j += i;
}
/*for (int m = 0; m < vec.size(); m++) {
cout << vec[m]<<" ";
}*/
//cout << endl;
int a = 0;
while (a < vec.size()) {
/*if (a == vec.size() - 1 && ) {
str.append(vec[vec.size() - 1]);
}*/
int temp = 1;
int b = a + 1;
while (b < vec.size() && vec[a] == vec[b] ) {
temp++;
b++;
}
if (temp == 1)
str.append(vec[a]);
else {
str.append(to_string(temp));
str.append(vec[a]);
}
//cout << str << endl;
a += temp;
}
//if(vec[vec.size() - 1].size()<i)
// str.append(vec[vec.size()-1]);
//cout << str << endl;
//cout << str.length() << endl;
if (answer > str.length())
answer = str.length();
}
return answer;
}
int main() {
string s;
cin >> s;
cout << solution(s);
return 0;
}
'Algorithm' 카테고리의 다른 글
[프로그래머스/C++] 서머코딩/윈터코딩(~2018) 쿠키 구입 (0) | 2020.03.09 |
---|---|
[프로그래머스/C++] 서머코딩/윈터코딩(~2018) 방문길이 (0) | 2020.02.19 |
[백준/C++] 14503 로봇 청소기 (0) | 2020.02.04 |
[백준/C++] 17487 타자 연습 (0) | 2020.01.28 |
[백준/C++] 1268 임시반장 정하기 (0) | 2020.01.22 |