【題解】ZeroJudge a743: 10420 – List of Conquests

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=a743
【解題想法】

  • 利用map會自動排序的特性,滿足題目output in alphabetical order的要求。
  • (Line-13) 清空資料緩衝區
  • (Line-16/17) getline() 讀進含空白的整行資料,只取the first word當作國家名稱。
#include <iostream>
#include <sstream>
#include <map>
using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    map <string, int> mp;
    string s;
    getline(cin, s);
    while (n--) {
        getline(cin, s);
        stringstream ss(s);
        ss >> s;
        mp[s]++;
    }
    for (auto i: mp) {
        cout << i.first << " " << i.second << "\n";
    }
    return 0;
}
分享本文 Share with friends