본 블로그는 비상업적, 비영리적 용도의 학업만을 위해 글을 게시합니다.
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int solution(string s) {
int answer = s.length();
int cut = 1;
for(;cut <= s.length() >> 1; cut++)
{
string before(s, 0, cut);
string result = "";
int count = 1;
for(int i = cut; i < s.length(); i += cut)
{
string now(s, i, min({cut, (int)s.length() - i}));
if(before == now)
count++;
else
{
if(count > 1)
result.append(to_string(count));
result += before;
before = now;
count = 1;
}
}
if(count > 1)
result.append(to_string(count));
result += before;
answer = min(answer, (int)result.length());
}
return answer;
}
#include <string>
#include <vector>
#include <iostream>
using namespace std;
vector<string> convert(string s, int n)
{
vector<string> v;
for(int i = 0 ; i <s.length() ; i+=n)
{
v.push_back(s.substr(i,n));
}
return v;
}
int solution(string s) {
int answer = 0;
vector<string> tok;
string before;
int cnt = 1;
int min = s.length();
string str="";
for(int j = 1 ; j <= s.length()/2 ; j++)
{
tok = convert(s,j);
str = "";
before = tok[0];
cnt = 1;
for(int i = 1 ; i < tok.size() ; i++)
{
if(tok[i] == before) cnt++;
else
{
if(cnt != 1) str += to_string(cnt);
str += before;
before = tok[i];
cnt = 1;
}
}
if(cnt != 1)str += to_string(cnt);
str += before;
min = min<str.length() ? min : str.length();
}
cout<<str;
return min;
}