0%

[題解]動物數量統計

a112. 動物數量統計

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
#include <bits/stdc++.h>
using namespace std;
map<string,queue<pair<string,int>>> mp;
//地點、動物名、數量
int main(){
int n;cin>>n;
vector<string> p;
while(n--){
string animal,pos;
int qu,ind;
cin>>animal>>qu>>pos;
ind = find(p.begin(),p.end(),pos)-p.begin();
if(ind==p.size())p.push_back(pos);
mp[pos].push({animal,qu});
}
for(auto i:p){
cout<<i<<":";
map<string,int> sta;
vector<string> almp;
while(!mp[i].empty()){
string an = mp[i].front().first;
int qu = mp[i].front().second;
mp[i].pop();
int ind = find(almp.begin(),almp.end(),an)-almp.begin();
if(ind==almp.size())almp.push_back(an);
sta[an]+=qu;
}
bool c = 0;
for(auto j:almp){
if(c)cout<<",";
cout<<j<<" "<<sta[j];
c = 1;
}
cout<<endl;
}
}