https://www.welcomekakao.com/learn/courses/30/lessons/60057

 

코딩테스트 연습 - 문자열 압축

데이터 처리 전문가가 되고 싶은 어피치는 문자열을 압축하는 방법에 대해 공부를 하고 있습니다. 최근에 대량의 데이터 처리를 위한 간단한 비손실 압축 방법에 대해 공부를 하고 있는데, 문자

www.welcomekakao.com

확인사항

숫자가 있으면 전부 1을 늘려주는 것으로 실수 했는데
공통 길이가 10일때 100일때 1000일때 처리해주면 된다.

#include <iostream>
#include <string>
#include <vector>

using namespace std;

struct li {
    string a;
    int b;
};

vector <li> list;

int solve(int n, string s) {
    list.clear();
    int chk = 0;

    string chkString = s.substr(0,n);
    string nString;
    int count = 1;
    for (int i = n; i <= s.size(); i += n) {
        nString = s.substr(i,n);
        if (chkString.size() != nString.size()) {
            list.push_back({ chkString,count });
            list.push_back({ nString,1 });
            break;
        }
        if (chkString == nString) {
            count++;
        }
        else if (chkString != nString) {
            list.push_back({ chkString,count });
            count = 1;
            chkString = nString;
        }
    }

    for (int i = 0; i < list.size(); i++)
    {
        chk += list[i].a.size();
        if (list[i].b > 1) {
            if (list[i].b >= 10 && list[i].b <= 99) {
                chk += 2;
            }
            else if (list[i].b >= 100 && list[i].b <= 999) {
                chk += 3;
            }
            else if(list[i].b==1000){
                chk += 4;
            }
            else {
                chk += 1;
            }
        }
    }
    return chk;
}

int solution(string s) {
    int answer = 0;
    answer = s.size();
    for (int i = 1; i <= s.size()/2+1; i++) {
        int a = solve(i, s);
        if (answer > a) {
            answer = a;
        }
    }

    return answer;
}

+ Recent posts