
map으로 푸는 문제. 살짝 헷갈렸다.
A부터 Z까지 m에 넣어준다.idx는 사전에 새로 추가되는 단어를 위한 인덱스이고,compString은 현재 비교할(사전과) 문자열이고, 현재 넣어줄 문자를 체크하는 게 compIndexcurString은 출력을 위한 변수로, 사전에 있는 경우 갱신한다.curString을 compString으로 갱신하고 문자를 계속 추가해준다.(사전에 없을 때까지)breakcompString)의 비교가 끝났을 때 curString의 사전 값을 출력한다.while문은 무한 반복문이기 때문에 현재 비교할 문자의 인덱스(compIndex)값이 msg의 사이즈보다 크거나 같으면 break 해준다.#include <string>
#include <vector>
#include <map>
#include <iostream>
using namespace std;
vector<int> solution(string msg) {
vector<int> answer;
map<string, int> m;
for(int i=0;i<26;i++)
{
string s = "";
s = 'A'+i;
m[s]=i+1;
}
int idx = 27;
int compIndex = 0;
string curString = "";
while(1)
{
string compString = "";
while(1)
{
if(compIndex>=msg.size()) break;
compString += msg[compIndex];
if(m.find(compString)!=m.end()) // 사전에 있으면
{
curString = compString;
}
else
{
m[compString] = idx;
idx++;
break;
}
compIndex++;
}
answer.push_back(m[curString]);
if(compIndex>=msg.size()) break;
}
return answer;
}