uva_10420List of Conquests
来源:互联网 发布:一级建造师网络培训 编辑:程序博客网 时间:2024/05/21 15:05
#include <cstdio>#include <cstring>#include <map>#include <iostream>#include <algorithm>using namespace std;#define MAX_CHAR 80#define MAXN 2001typedef struct NODE_ { int index; string country;}NODE;NODE node[MAXN];int n, idx, rst[MAXN];int cmp(const NODE &a, const NODE &b){ return a.country < b.country;}int main(int argc, char const *argv[]){#ifndef ONLINE_JUDGE freopen("test.in", "r", stdin);#endif string name; map<string, int> hash; map<string, int>::iterator it; char text[MAX_CHAR], t_name[MAX_CHAR]; while( ~scanf("%d", &n) ) { memset(rst, 0, sizeof(rst)); getchar(), idx = 0; for(it = hash.begin(); it != hash.end(); it ++) { hash.erase(it); } for(int i = 0; i < n; i ++) { gets(text); sscanf(text, "%s", t_name); name = t_name; if( hash.end() == (it = hash.find(name)) ) { hash.insert(map<string, int>::value_type(name, idx ++)); continue; } rst[ it->second ] ++; } idx = 0; for(it = hash.begin(); it != hash.end(); it ++) { node[idx].index = it->second; node[idx ++].country = it->first; } sort(node, node+idx, cmp); for(int i = 0; i < idx; i ++) { printf("%s %d\n", node[i].country.c_str(), rst[ node[i].index ]+1); } } return 0;}