#include <string>
#include <vector>
#include <map>
using namespace std;
vector<int> solution(vector<string> enroll, vector<string> referral, vector<string> seller, vector<int> amount) {
vector<int> answer(enroll.size());
map<string,string> parent;
map<string,int> number_map;
for(int i=0;i<referral.size();i++)
{
parent[enroll[i]] = referral[i];
number_map[enroll[i]] = i;
}
for(int i=0;i<seller.size();i++)
{
int price = 100 * amount[i];
int part = price/10;
string next = parent[seller[i]];
int n = number_map[seller[i]];
answer[n] += price - part;
price = part;
part = price/10;
while(price > 0 and next != "-")
{
n = number_map[next];
answer[n] += price - part;
price = part;
part = price/10;
next = parent[next];
}
}
return answer;
}
- 핵심
map
을 이용해서 parent
와 인덱스
를 관리
해서 문제 해결