【題解】ZeroJudge e839: P6. 飲食分類 (Food)

【題目敘述】https://zerojudge.tw/ShowProblem?problemid=e839
TOI 新手同好會 歷屆考題

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

int n;
map <string, vector<string> > mp;
string s1, s2;

int main() {
    cin >> n;
    for (int i = 0; i < n; i++){
        cin >> s1 >> s2;
        mp[s2].push_back(s1);
    }
    cin >> s2;
    if (!mp[s2].size()) cout << "No\n";
    else{
        sort(mp[s2].begin(), mp[s2].end());
        for (auto i:mp[s2]){
            cout << i << "\n";
        }
    }
}

Python code (credit: Amy Chou)

N = int(input())
dic = {}
for _ in range(N):
    F, S = map(str, input().split())
    if S in dic:
        dic[S].append(F)
    else:
        dic[S] = [F]

target = input()
if target in dic:
    for i in sorted(dic[target]):
        print(i)
else:
    print("No")
分享本文 Share with friends