In Act I, Leporello is telling Donna Elvira about his master's long list of conquests:
``This is the list of the beauties my master has loved, a list I've made out myself: take a look, read it with me. In Italy six hundred and forty, in Germany two hundred and thirty-one, a hundred in France, ninety-one in Turkey; but in Spain already a thousand and three! Among them are country girls, waiting-maids, city beauties; there are countesses, baronesses, marchionesses, princesses: women of every rank, of every size, of every age.'' (Madamina, il catalogo è questo)
As Leporello records all the ``beauties'' Don Giovanni ``loved'' in chronological order, it is very troublesome for him to present his master's conquest to others because he needs to count the number of ``beauties'' by their nationality each time. You are to help Leporello to count.
3 Spain Donna Elvira England Jane Doe Spain Donna Anna
England 1 Spain 2
python:
while True: try: n=int(input()) dic={} for _ in range(n): a=[x for x in input().split()] a1=a[0] if(a1 in dic): dic[a1]+=1 else: dic[a1]=1 dic1=sorted(dic.items(),key=lambda x:x[0])#根據key質排序 """ 這裡要注意:使用sorted後,他返回的是一個list [('England', 1), ('Spain', 2)] 所以要搭配list的for寫法 """ for k in dic1: print(k[0],k[1]) """ 若今天是純dic: for k,v in dic.items(): print(k,v) """ except: break
C++:
/* Sample Input #1 3 Spain Donna Elvira England Jane Doe Spain Donna Anna Sample Output #1 England 1 Spain 2 */ #include <iostream> #include <sstream> #include <map> using namespace std; int main(){ //ios::sync_with_stdio(0); //cin.tie(0); int n; string s; cin>>n; getline(cin,s); map<string,int> mp; while(n--){ getline(cin,s); stringstream ss(s); ss>>s;//取出地名 mp[s]++; } for(auto i:mp){ cout<<i.first<<" "<<i.second<<"\n"; } return 0; }