슈뢰딩거의 고등어
[프로그래머스] 문자열 압축 본문
https://programmers.co.kr/learn/courses/30/lessons/60057?language=cpp
- 자르는 단위가 문자열길이의 /2 이상일 경우, 압축이 되지않으므로 1부터 s/2 까지 확인해보면 된다.
- 자르는 단위
- 문자열 처음부터 끝까지 확인 ( 자르는 문자열 (키) 가 다음에 존재할 경우, cnt++ 해주고
- 존재하지 않을 경우, 압축된 결과를 result 에 append 해준다.
- cnt == 1 일경우, cnt 는 append 안해줘도 됨
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int solution(string s) {
int answer = s.size();
int len = answer;
int n = len/2;
string result = "";
for(int i=1; i<=n; i++) {
result = "";
string ss = s.substr(0, i);
int cnt = 1;
for(int j=i; j<=len; j+=i) {
if (ss == s.substr(j,i)){
cnt+=1;
}
else {
if (cnt == 1) {
result += ss;
}
else {
result += (to_string(cnt)+ss);
}
/* 일치하지 않을 경우, 키를 탐색위치의 것으로 바꿔준다 */
ss = s.substr(j, i);
cnt = 1;
}
}
if ((len % i) != 0)
result += s.substr((len/i)*i);
if (answer > result.size()) {
answer = result.size();
}
}
cout << result;
return answer;
}
'알고리즘' 카테고리의 다른 글
[프로그래머스] 없는 숫자 더하기 (0) | 2022.02.08 |
---|---|
[프로그래머스] 신규아이디 추천 (0) | 2022.02.08 |
[프로그래머스] 행렬 테두리 회전하기 (0) | 2022.02.08 |
[프로그래머스] 로또의 최고 순위와 최저순위 (0) | 2022.02.08 |
[프로그래머스] 로또의 최고 순위와 최저순위 (0) | 2022.02.08 |
Comments