AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 1. Elementary Problem Solving ::Sorting/Searching
// 10420 - List of Conquests
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
struct data
{
char country[20];
int times;
data(){times=0;}
};
data A[2000];
int cmp(const void* a, const void* b)
{
data* _a = (data*)a;
data* _b = (data*)b;
if(strcmp(_a->country, _b->country) < 0) return -1;
if(strcmp(_a->country, _b->country) == 0) return 0;
if(strcmp(_a->country, _b->country) > 0) return 1;
}
int main(void)
{
int i, j, n, s;
char line[60], countries[20], name[40];
cin >> n;
getchar();
s = 0;
for(i=0; i<n; i++)
{
cin.getline(line, 60);
sscanf(line, "%s", countries);
strcpy(name, line+strlen(countries)+1);
for(j=0; j<s; j++) // 寻找是否有重复的
if(strcmp(A[j].country, countries) == 0)
{
A[j].times++;
break;
}
if(j == s)
{
strcpy(A[j].country, countries);
A[j].times++;
s++;
}
}
qsort(A, s, sizeof(A[0]), cmp);
for(i=0; i<s; i++)
cout << A[i].country << " " << A[i].times << endl;
return 0;
}