알고리즘/프로그래머스
[2018 KAKAO BLIND RECRUITMENT 3차] 압축
동 코
2020. 4. 24. 18:49
문제풀이
map을 이용하여서 새로운 문자열과 인덱스를 저장한다.
구현과정은 문제에 설명한대로 따라 짜면 무난하다.
소스코드
더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
#include <string>
#include <vector>
#include <map>
#include <iostream>
using namespace std;
map<string,int> dict;
void initDictionary(){
for(int i=0;i<26;i++){
char alphabet = 'A'+i;
string inputString(1,alphabet);
dict[inputString] = i+1;
}
}
vector<int> solution(string msg) {
vector<int> answer;
initDictionary(); //길이가 1인 모든단어를 포함하도록 사전을 초기화.
int i=0;
while( i<msg.size()){
string str(1,msg[i]);
string nextStr = str;
int j;
for(j=i+1;j<msg.size();){
nextStr +=msg[j];
break;
}
else{
str = nextStr;
j++;
}
}
answer.push_back(dict[str]);//결과 벡터에 최대 길이 삽입
//cout<<str<<","<<dict[str]<<endl;
//j++;
str += msg[j];
//cout<<str<<endl;
int idx = dict.size()+1;
//cout<<idx<<endl;
dict[nextStr] = idx; // 해당문자열 + 그다음글자 해서 등록
i = j; //
}
return answer;
}
|
문제 링크 : https://programmers.co.kr/learn/courses/30/lessons/17684
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr