set 和 map的嵌套应用,很搞笑的是typedef map<string, set<string>> Record;这样在windows下面编译可以过,但是跑到服务器上面的gnu g++就编译过不了了,要这样:typedef map<string, set<string> > Record; (就少了一个空格,也不至于编译报错这么严重吧?),下面是AC代码:
#include <stdio.h>
#include <set>
#include <map>
#include <string.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAX_LEN 80
typedef map<string, set<string> > Record;
Record record;
char buffer[MAX_LEN];
set<string> name_set;
int main(void)
{
int n;
char country[MAX_LEN];
char name[MAX_LEN];
string name_string, country_string;
char *p;
map<string, set<string> >::iterator it;
gets(buffer);
sscanf(buffer, "%d", &n);
while(n--)
{
gets(buffer);
p = strstr(buffer, " ");
p[0] = '\0';
strcpy(country, buffer);
p++;
while(1)
{
if(*p != ' ')
break;
p++;
}
strcpy(name, p);
name_string.assign(name);
country_string.assign(country);
//cout << "country:" << country_string << " name:" << name_string << endl;
it = record.find(country_string);
if(it == record.end())
{
name_set.clear();
record[country_string] = name_set;
record[country_string].insert(name_string);
}
else
{
it->second.insert(name_string);
}
}
for(it=record.begin(); it!=record.end(); it++)
{
cout << it->first << " " << it->second.size() << endl;
}
return 0;
}