Make Unreal REAL.
article thumbnail
Level 2. [3차] 압축

 

 

문제가 길어서 뭔가 번거로운 문제인 줄 알았는데 이해하고 나니 그렇게 어려운 문제는 아니었다.

 

문제에 나와 있는대로 차근차근 구현하니 잘 풀 수 있었다.

 

#include <iostream>
#include <vector>
#include <deque>
#include <unordered_map>

using namespace std;

vector<int> solution(string msg)
{
    vector<int> answer;
    int cur = 1;
    unordered_map<string, int> dict;
    deque<char> Msg(msg.begin(), msg.end());

    for (char c = 'A'; c <= 'Z'; ++c)
        dict[string(1, c)] = cur++;

    while (!Msg.empty())
    {
        string s(1, Msg.front()); Msg.pop_front();
        
        while (!Msg.empty() && dict[s + Msg.front()])
            s += Msg.front(), Msg.pop_front();

        answer.emplace_back(dict[s]);

        if (!Msg.empty())
            dict[s + Msg.front()] = cur++;
    }

    return answer;
}
profile

Make Unreal REAL.

@diesuki4

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

검색 태그