---
# System prepended metadata

title: 'a112: 動物數量統計'
tags: [結構]

---

---
tags: 結構
---

# a112: 動物數量統計

``` cpp=
#include <iostream>
#include <string>
#include <vector>

using namespace std;

struct Animal {
    string name;
    int cnt;
};

struct Place {
    string name;
    vector<Animal> animals;
};

int main()
{
    int n, amount, i;
    string animal, place;

    vector<Place>   place_list;

    for (cin >> n; n > 0; n--) {
        cin >> animal >> amount >> place;
        
        for (i = 0; i < place_list.size() && place_list[i].name != place; i++) ;
        
        if (i >= place_list.size()) {
            place_list.push_back({place, {}});
        }
        
        Place &p = place_list[i];
        
        for (i = 0; i < p.animals.size() && p.animals[i].name != animal; i++) ;
        
        if (i >= p.animals.size())
            p.animals.push_back({animal, 0});
            
        p.animals[i].cnt += amount;
    }
    
    for (auto &p: place_list) {
        cout << p.name << ':';
        
        for (i = 0; i < p.animals.size(); i++) {
            if (i > 0)
                cout << ',';

            cout << p.animals[i].name << ' ' << p.animals[i].cnt;
        }
        cout << '\n';
    }
    return 0;
}
```