압축된 갯수의 len도 고려해야되서 귀찮았음.
queue로 썻으면 더 빨리 풀었을꺼 같다.
#include <string>
#include <vector>
using namespace std;
int CNT_LEN(int n)
{
if (n == 1000)
return 4;
else if (n >= 100)
return 3;
else if (n >= 10)
return 2;
else if (n >= 2)
return 1;
else
return 0;
}
int STRING_ZIP(int n, string s)
{
vector<string> split;
int ind = 0;
int len = 0;
for (int i = 0; i < s.size(); i += n)
split.push_back(s.substr(i, n));
int cnt = 1;
while (split.size() != 1)
{
if (split[0] == split[1])
cnt++;
else
{
len += n + CNT_LEN(cnt);
cnt = 1;
}
split.erase(split.begin(), split.begin() + 1);
}
return len + CNT_LEN(cnt) + split[0].size();
}
int solution(string s) {
int answer = s.size();
for (int i = 1; i < s.size(); i++)
{
int temp = STRING_ZIP(i, s);
if (answer > temp)
answer = temp;
}
return answer;
}'Coding_Test 연습 > Programmers' 카테고리의 다른 글
| [프로그래머스] (C++) LV2 멀쩡한 사각형 (0) | 2022.04.28 |
|---|---|
| [프로그래머스] (C++) LV2 오픈채팅방 (0) | 2022.04.28 |
| [프로그래머스] (C++) LV1 직사각형 별찍기 (0) | 2022.04.27 |
| [프로그래머스] (C++) LV1 x만큼 간격이 있는 n개의 숫자 (0) | 2022.04.27 |
| [프로그래머스] (C++) LV1 행렬의 덧셈 (0) | 2022.04.27 |