批量初始化次数-拓扑排序+动态规划
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
string a[5];
map<pair<string, string>, int> h1;
set<string> ses;
while(n --)
{
string s, t;
cin >> s;
int k = 0;
stringstream ss(s);
while(getline(ss, t, ','))
a[k ++] = t;
string st = a[0] + a[1] + a[2];
if(ses.count(st)) continue;
else ses.insert(st);
int tt = stoi(a[3]);
if(tt < 0 || tt > 100) continue;
h1[{a[1], a[2]}] += tt;
}
int m;
cin >> m;
unordered_map<string, int> h2;
while(m --)
{
string s, t;
cin >> s;
stringstream ss(s);
int k = 0;
while(getline(ss, t, ','))
a[k ++] = t;
h2[a[0]] = stoi(a[1]);
}
map<string, int> h3;
for(auto &[k, v] : h1)
{
string client = k.first, factor = k.second;
h3[client] += (h2[factor] * v);
}
vector<pair<string, int>> res;
for(auto &[k, v] : h3) res.push_back({k, v});
sort(res.begin(), res.end());
for(int i = 0; i < res.size(); i ++) cout << res[i].first << ',' << res[i].second << endl;
return 0;
}