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;
}
'자료구조 & 알고리즘 > 프로그래머스' 카테고리의 다른 글
Level 2. 땅따먹기 (0) | 2023.06.06 |
---|---|
Level 2. 주식가격 (0) | 2023.06.05 |
Level 2. k진수에서 소수 개수 구하기 (0) | 2023.06.03 |
Level 2. [3차] n진수 게임 (0) | 2023.06.02 |
Level 2. 점프와 순간 이동 (0) | 2023.06.01 |