1. 

#include <string>
#include <vector>
#include <map>
#include <iostream>
using namespace std;
 
vector<int> solution(string msg) 
{
    vector<int> answer;
    map<stringint> dic;
    
    for(int i = 0; i < 26; i++)
    {
        char alphabet = 'A' + i;
        string temp;
        temp += alphabet;
        
        dic[temp] = i+1;
    }
 
    string now;
    for(int i = 0; i < msg.size(); i++)
    {
        now += msg[i];
        
        // 사전에서 못찾은 경우
        if(dic.find(now) == dic.end())
        {
            // 사전에 입력 추가
            dic[now] = dic.size() + 1;
            
            // 한 단계 전의 입력을 출력해야되기 때문에 맨 뒤 문자 삭제
            now.pop_back();
            
            // 출력
            answer.push_back(dic[now]);
            
            // 초기화
            now.clear();
            
            i--;
        }      
    }
    
    // 남은 입력
    if(now.size() > 0)
    {
        answer.push_back(dic[now]);
    }  
    
    return answer;
}
cs

 

2.

#include <string>
#include <vector>
#include <iostream>
using namespace std;
 
vector<int> solution(string msg) 
{
    vector<int> answer;
    vector<pair<stringint>> dic;
    
    for(int i = 0; i < 26; i++)
    {
        char alphabet = 'A' + i;
        string temp;
        temp += alphabet;
        
        dic.push_back({temp, i+1});
    }
    
    int nowIdx = 0;
    while(nowIdx != msg.size())
    {
        string now;
        bool find = true;
 
        while(find == true)
        {
            // 마지막까지 남아있는 문자열 처리를 위한 구문
            if(nowIdx == msg.size())
            {
                for(int i = 0; i < dic.size(); i++)
                {
                    if(now == dic[i].first)
                    {
                        answer.push_back(dic[i].second);
                        return answer;
                    }
                }
            }
            
            find = false;
            now += msg[nowIdx];
            
            // 사전에서 입력을 찾는 과정
            for(int i = 0; i < dic.size(); i++)
            {
                if(now == dic[i].first)
                {
                    find = true;
                    break;
                }
            }
            
            // 입력을 찾지 못한 경우
            if(find == false)
            {
                // 사전추가
                dic.push_back({now, dic.size()+1});
                
                // 한 단계 전의 입력을 출력해야되기 때문에 맨 뒤 문자 삭제
                now.pop_back();
                
                for(int i = 0; i < dic.size(); i++)
                {
                    if(now == dic[i].first)
                    {
                        // 출력
                        answer.push_back(dic[i].second);
                        break;
                    }
                }
            }
            // 입력을 찾은 경우 : 인덱스 증가
            else
            {
                nowIdx += 1;
            }
        }
    }      
    
    return answer;
}
cs

+ Recent posts