Skip to content

Instantly share code, notes, and snippets.

@DongguemYoo
Created May 22, 2020 05:25
Show Gist options
  • Save DongguemYoo/8213d6d73f84372308eead9646e0bbd2 to your computer and use it in GitHub Desktop.
Save DongguemYoo/8213d6d73f84372308eead9646e0bbd2 to your computer and use it in GitHub Desktop.
코딩테스트 연습 KAKAO BLIND_ 문자열 압축
일단 cpp로 해야해서 잘 모르는 문법 검색좀 했음
//string에 int를 더할때 그냥 더해서 에러가 났음
//to_string으로 더하니까 모든 케이스 통과했음
#include <string>
#include <vector>
using namespace std;
int solution(string s) {
int answer = 0;
int checkLen =0;
string target;
string result;
answer = s.size();
for(int x=1;x<s.size();x++)
{
target = s.substr(0,x);
result.clear();
checkLen =0;
for(int i=0;i<s.size();i+=x)
{
if(target == s.substr(i,x))
{
checkLen++;
}else
{
if(checkLen == 1)
{
result+= target;
}else
{
result+= to_string(checkLen);
result+= target;
}
target = s.substr(i,x);
checkLen =1;
}
}
if (checkLen == 1)
{
result += target;
}
else
{
result += to_string(checkLen);
result += target;
}
if(result.size() < answer)
{
answer = result.size();
}
}
return answer;
}
//내 문제점
if (checkLen == 1)
{
result += target;
}
else
{
result += to_string(checkLen);
result += target;
}
이 구간을
if(checkLen !=1 )
result += to_string(checkLen);
result += target;
이런식으로 변경할 수 있었는데...ㅋㅋㅋ 바보같이 구현함
중복이 있는것을 잘 보도록 하자
//다른사람들의 풀이와 확연히 다른점
for(int x=1;x<s.size();x++) 이 부분에서
다른사람들은
for(int x=1;x<=s.size()/2;x++) 로 표현함
패턴을 찾는것이기 때문에 반만 찾으면 된다...라고함
적용하려면 반드시 x<=size()/2 로 표현해주어야함
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment