프로그래머스 - [3차] 압축

well-life-gm·2021년 12월 21일
0

프로그래머스

목록 보기
99/125

프로그래머스 - [3차] 압축

map정도만 쓰면 되는 간단한 구현 문제이다. 예외케이스도 딱히 없다.

코드는 아래와 같다.

#include <string>
#include <vector>
#include <map>
#include <cstdio>
#include <iostream>

using namespace std;

vector<int> solution(string msg) {
    vector<int> answer;
    
    map<string, int> m;
    int cnt = 26;
    for(int i=1;i<=cnt;i++) {
        string tmp;
        tmp.push_back(i - 1 + 'A');
        m.insert({tmp, i});
    }
    for(int i=0;i<msg.size();i++) {
        string tmp;
        int start = i;
        while(1) {
            if(start > msg.size())
                break;
            tmp.push_back(msg[start]);
            if(m.find(tmp) == m.end()) {
                cnt++;
                m.insert({tmp, cnt});
                tmp.pop_back();
                answer.push_back(m[tmp]);
                break;
            }
            start++;
        }
        i = start - 1;
    }
    
    return answer;
}

결과

profile
내가 보려고 만든 블로그

0개의 댓글