[Programmers] 문자열 압축

Time Lapse :NONE

solution.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int solution(string s) {
int answer = s.length();
for(int i=1;i<=s.length()/2;i++)
{
string new_s;
int idx = 0;

while(1)
{
string comp=s.substr(idx,i);
int cnt = 0;
while(comp.compare(s.substr(idx,i))==0)
{
cnt++;
idx+=i;
}
if(cnt>1)
{
new_s+=to_string(cnt);
new_s+=comp;
}
else {
new_s+=comp;
}
if(idx+i>=s.length()) {
new_s+=s.substr(idx,s.length()-idx);
break;
}
}
answer = min(answer,(int)new_s.length());
}

return answer;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/Programmers/stringZIP/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.