ZeroJudge A743: List of Conquests

UVa 10420 – List of Conquests

Each set of data starts with a positive integer N. Following that, there will be N lines, with each line starting with a country name, followed by a person's name. You are required to output the number of occurrences of each country.

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
The first line contains a positive integer N. Following that, there will be N lines, each containing three strings: a country name, a person's first name, and a person's last name.Output the number of occurrences of each country, and sort the output by the alphabetical order of the country names.
3
Spain Donna Elvira
England Jane Doe
Spain Donna Anna
England 1
Spain 2

Thought Process

When inputting, you can use three strings as input. The crucial point is that you can ignore the remaining names after the country name.

Use a map to record the occurrence count of each country. When outputting, you can use for (auto it : Map), allowing you to output in alphabetical order.

Sample Code-ZeroJudge A743: List of Conquests

#include <iostream>
#include <map>
using namespace std;

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    int N;
    cin >> N;
    string a;
    getline(cin, a);
    map<string, int>MAP;
    for (int i = 0; i<N; i++)
    {
        string str;
        getline(cin, str);
        string country = "";
        for (int j = 0; j<str.length(); j++)
        {
            if (str[j] == ' ') break;
            country += str[j];
        }
        MAP[country]++;
    }
    for (auto it:MAP)
    {
        cout << it.first << " " << it.second << "\n";
    }
}

//ZeroJudge A743
//Dr. SeanXD

Comments