Here's a sample implementation:
000 #include <stdio.h>
001 #include <map>
002 #include <iostream>
003 #include <string>
004
005 using namespace std;
006
007 int main() {
008 char line[100], ctmp[100];
009 int n, i, j;
010 map<string, int> countries;
011 map<string, int>::iterator iter;
012
013 fgets(line, 100, stdin);
014 sscanf(line, "%d", &n);
015
016 for (i = 0; i < n; i += 1) {
017 fgets(line, 100, stdin);
018
019 for (j = 0; 1; j += 1) {
020 if (line[j] == ' ') {
021 break;
022 }
023 ctmp[j] = line[j];
024 }
025 ctmp[j] = 0;
026
027 countries[ctmp] += 1;
028 }
029
030 for (iter = countries.begin(); iter != countries.end(); ++iter) {
031 cout << iter->first << " " << iter->second << endl;
032 }
033
034 return 0;
035 }
No comments:
Post a Comment