https://school.programmers.co.kr/learn/courses/30/lessons/17684
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
#include <string>
#include <vector>
#include <unordered_map>
#include <iostream>
using namespace std;
unordered_map<string, int> m;
string s;
int mSize;
void initMap() {
string str = "A";
for (int i = 1; i <= 26; i++) {
m[str] = i;
str[0] = 'A' + i;
}
}
int getSizeAddString(string s) { // 더할 문자열의 길이를 리턴
int curSize = 1;
string addString;
while (curSize <= s.size()) {
if (m.find(s.substr(0, curSize)) == m.end()) { // 없는게 나오면
addString = s.substr(0, curSize);
break;
}
curSize++;
}
m[addString] = ++mSize;
return curSize - 1;
}
vector<int> solution(string msg) {
s = msg;
mSize = 26;
initMap();
vector<int> dict;
int i = 0;
while (i < s.size()) {
int size = getSizeAddString(s.substr(i));
dict.push_back(m[s.substr(i, size)]);
i += size;
}
return dict;
}
중간에 오류 도저히 못찾겠어서 챗쥐피티한테 물어보니까 curSize -1 안해줘서 생기는 문제였다... 심지어 리팩토링도 엄청 깔끔하게 해 준다. 인공지능의 발전 멈춰!!
'<algorithm> > 프로그래머스' 카테고리의 다른 글
프로그래머스 단어 변환 c++ (0) | 2023.08.30 |
---|---|
프로그래머스 [3차] n진수 게임 c++ (0) | 2023.08.30 |
프로그래머스 이중우선순위큐 c++ (0) | 2023.08.06 |
프로그래머스 야근 지수 c++ (0) | 2023.07.30 |
프로그래머스 네트워크 c++ (2) | 2023.07.03 |