https://school.programmers.co.kr/learn/courses/30/lessons/160586
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
문자 별로 cnt 값을 map에 저장해 두고 target을 순회하면서 더해준다.
#include <string>
#include <vector>
#include <unordered_map>
#include <iostream>
using namespace std;
vector<int> solution(vector<string> keymap, vector<string> targets) {
vector<int> answer;
unordered_map<char, int> map;
for (int i = 0; i < keymap.size(); i++)
{
for (int j = 0; j < keymap[i].size(); j++)
{ // map에 문자를 key로 횟수를 value로 넣어준다
if (map.find(keymap[i][j]) == map.end() || (map[keymap[i][j]] > j + 1))
map[keymap[i][j]] = j + 1;
// cout << "key: " << keymap[i][j] << " value: " << map[keymap[i][j]] << '\n';
}
}
for (int i = 0; i < targets.size(); i++)
{
int sum = 0;
for (int j = 0; j < targets[i].size(); j++)
{
if (map.find(targets[i][j]) == map.end())
{ // 없는 값일때 처리
sum = -1;
break ;
}
sum += map[targets[i][j]];
}
answer.push_back(sum);
}
return answer;
}
이건 좀 잘 푼 것 같다. 나 자신 칭찬해~
이번 문제부터 크롬 확장프로그램인 백준 허브로 깃헙에 자동 업로드 되게 해 놓았다. 문제 링크도 있고 런타임도 나와서 아주 간편하다.
'<algorithm> > 프로그래머스' 카테고리의 다른 글
프로그래머스 성격 유형 검사하기 c++ (0) | 2023.05.24 |
---|---|
프로그래머스 신고 결과 받기 c++ (0) | 2023.05.24 |
프로그래머스 공원 산책 c++ (0) | 2023.05.22 |
프로그래머스 덧칠하기 c++ (0) | 2023.05.22 |
프로그래머스 추억 점수 c++ (3) | 2023.05.21 |