hash,使用stl map ac。学了find_if等强大的东西,第一次使用stl模板。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
using namespace std; #define NAMENUM 25 typedef struct fruit_st {
char name[NAMENUM];
char produce[NAMENUM];
bool operator < (const fruit_st &a) const {
if (strcmp(produce, a.produce))
return strcmp(produce, a.produce)> ? false : true;
else
return strcmp(name, a.name)> ? false : true;
}
} fruit_st; class map_finder {
public:
map_finder(const fruit_st a) {
strcpy(fruit.name, a.name);
strcpy(fruit.produce, a.produce);
}
bool operator ()(const map<fruit_st, int>::value_type &pair) {
if (strcmp(fruit.name, pair.first.name)== && strcmp(fruit.produce, pair.first.produce)==)
return true;
else
return false;
}
private:
fruit_st fruit;
}; int main() {
map<fruit_st, int> sale_info;
map<fruit_st, int>::iterator iter;
fruit_st fruit;
int case_n, n, num; scanf("%d", &case_n); while (case_n--) {
scanf("%d", &n);
while (n--) {
scanf("%*c%s %s %d", fruit.name, fruit.produce, &num); iter = find_if(sale_info.begin(), sale_info.end(), map_finder(fruit));
if (iter == sale_info.end()) {
sale_info.insert(make_pair(fruit, num));
//sale_info[fruit] = num;
}else
iter->second += num;
}
for (map<fruit_st, int>::iterator ii=sale_info.begin(); ii!=sale_info.end(); ++ii) {
if (ii != sale_info.begin()) {
if (strcmp(ii->first.produce, iter->first.produce))
printf("%s\n", ii->first.produce);
printf(" |----%s(%d)\n", ii->first.name, ii->second);
} else {
printf("%s\n", ii->first.produce);
printf(" |----%s(%d)\n", ii->first.name, ii->second);
}
iter = ii;
}
sale_info.clear();
if (case_n)
printf("\n");
} return ;
}
04-25 18:59