본 블로그는 비상업적, 비영리적 용도의 학업만을 위해 글을 게시합니다.
#include <string>
#include <vector>
#include <map>
#include <iostream>
using namespace std;
bool isRegistered(map<string, int> LZW, string key)
{
if(LZW.count(key))
return true;
return false;
}
vector<int> solution(string msg) {
int value = 27, length = 1;;
vector<int> answer;
map<string, int> LZW;
for(int i = 0; i < 26;)
{
string s(1, 'A' + i);
LZW[s] = ++i;
}
for(int i = 0; i < msg.length();)
{
string key(1, msg[i]);
while(isRegistered(LZW, key))
{
key += msg[++i];
if(i == msg.length())
break;
}
if(isRegistered(LZW, key))
{
answer.push_back(LZW[key]);
}
else
{
LZW[key] = value++;
answer.push_back(LZW[key.substr(0, key.length() - 1)]);
}
}
// for(auto i : LZW)
// cout << '{' << i.first << ':' << i.second << "} ";
// cout << '\n';
// for(auto i : answer)
// cout << i << ' ';
// cout << '\n';
return answer;
}