260320 TIL - 챕터4 멀티플레이어 게임 개발 10일차

2026. 3. 20. 23:45언리얼 7기 본캠프

간만에 코드카타 2문제 루틴으로 돌아옴!

//압축 문제. LZW 압축을 구현하는 문제라는데 자세한 내용까지는 이해하지는 못했고 문제 설명 위주로 구현함
//문자열이 파면 팔수록 심오한게 많아져서 문자열 파싱 및 저장을 좀 효율적으로 하는걸
//염두에 두면서 문제를 풀면 좋을 것 같음
#include <string>
#include <vector>
#include <unordered_map>

using namespace std;

vector<int> solution(string msg) {
    vector<int> answer;
    unordered_map<string, int> dict;
    for(int i = 0; i < 26; i++)
    {
        string s(1, 'A' + i);
        dict[s] = i + 1;
    }
    int nextindex = 27;
    
    for(int i = 0; i < msg.length();)
    {
        int checklength = 1;
        int foundlength = 1;
        while(true)
        {
            if (i + checklength > msg.length()) break;
            string checksubstr = msg.substr(i, checklength);
            auto it = dict.find(checksubstr);
            if (it == dict.end())
            {
                dict[checksubstr] = nextindex;
                nextindex++;
                break;
            }
            foundlength = checklength;
            checklength++;
        }
        string foundstr = msg.substr(i, foundlength);
        answer.push_back(dict[foundstr]);
        i += foundlength;
    }
    return answer;
}

//파일명 정렬 문제. stable_sort의 존재에 대해서 알게 되었고, 람다 함수에서 조금 더 자세한 정렬 조건을
//작성해볼 수 있는 문제였음. 아직도 sort의 구성이나 람다 작성법이 헷갈리긴 한데,
//그래도 상당히 유용한 문제였음. string 다루는것도 아주 조금은 더 능숙해진 듯?
#include <string>
#include <vector>
#include <algorithm>
#include <cctype>

using namespace std;

struct namephrase{
    string head;
    string number;
};

namephrase tostruct(const string& s){
    namephrase newstruct;
    int phase = 0;
    string head = "", number = "";
    for(int i = 0; i < s.length(); i++)
    {
        if (phase == 0)
        {
            if (isdigit(s[i]))
            {
                phase++;
                number += s[i];
            }
            else head += tolower(s[i]);
        }
        else if (phase == 1)
        {
            if (!isdigit(s[i]) || number.size() == 5) break;
            else number += s[i];
        }
    }
    newstruct.head = head;
    newstruct.number = number;
    return newstruct;
}

vector<string> solution(vector<string> files) {
    vector<string> answer = files;
    
    stable_sort(answer.begin(), answer.end(), [](const string& a, const string& b){
        namephrase Aphrase = tostruct(a);
        namephrase Bphrase = tostruct(b);
        string Ahead = Aphrase.head;
        string Bhead = Bphrase.head;
        int Anum = stoi(Aphrase.number);
        int Bnum = stoi(Bphrase.number);
        
        if (Ahead != Bhead) return Ahead < Bhead;
        if (Anum != Bnum) return Anum < Bnum;
        return false;
    });
    
    return answer;
}