hdu 1263 水题一道

水题,不过还是挺考查对STL的熟悉程度的,我用的set做,居然也用了好长时间。

/*
* hdu1263/win.cpp
* Created on: 2011-10-13
* Author : ben
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std;
typedef struct {
char name[90];
char addr[90];
int num;
} Fruit;

bool inline operator<(const Fruit &f1, const Fruit &f2) {
int r1 = strcmp(f1.addr, f2.addr);
if (r1 != 0) {
return r1 < 0;
} else {
return strcmp(f1.name, f2.name) < 0;
}
}

bool inline operator==(const Fruit &f1, const Fruit &f2) {
return (!strcmp(f1.name, f2.name)) && (!strcmp(f1.addr, f2.addr));
}

int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
int T, M;
char lastaddr[100];
scanf("%d", &T);
while (T--) {
set<Fruit> myset;
scanf("%d", &M);
Fruit temp;
for (int i = 0; i < M; i++) {
scanf("%s %s %d", temp.name, temp.addr, &temp.num);
if (myset.count(temp) != 0) {
temp.num += (*(myset.find(temp))).num;
myset.erase(temp);
}
myset.insert(temp);
}
set<Fruit>::iterator it;
it = myset.begin();
memset(lastaddr, 0, sizeof(lastaddr));
while (it != myset.end()) {
if (strcmp((*it).addr, lastaddr) != 0) {
strcpy(lastaddr, (*it).addr);
puts((*it).addr);
}
printf(" |----%s(%d)\n", (*it).name, (*it).num);
it++;
}
if (T > 0) {
putchar('\n');
}
}
return 0;
}



posted @ 2011-10-13 13:10  moonbay  阅读(174)  评论(0编辑  收藏  举报