hdu 1263 水果

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1263

简单题,练习一下vector,具体如下:

#include<iostream>
#include<vector>
#include <cstring>
#include<algorithm>
#define rt place[i].val
const int Max_N = 110;
typedef char State[82];
struct node{ State list; int val; }x1, x2;
std::vector<node> rec[Max_N], place;
int cmp(const node & a, const node & b){
	return strcmp(a.list, b.list) == -1 ? 1 : 0;
}
bool is_exist(std::vector<node> ve, char *pls){
	for (int i = 0; i < ve.size(); i++){
		if (0 == strcmp(pls, ve[i].list)) return true;
	}
	return false;
}
int main(){
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w+", stdout);
#endif
	int t, n, i, k;
	scanf("%d", &t);
	while (t--){
		k = 0;
		scanf("%d", &n);
		for (i = 0; i < n; i++){
			scanf("%s %s %d", x2.list, x1.list, &x2.val);
			if (0 == place.size() || !is_exist(place, x1.list)){
				x1.val = k;
				place.push_back(x1), rec[k++].push_back(x2);
			} else {
				int flag = 0, d = 0;
				for (int j = 0; j < place.size(); j++){
					if (0 == strcmp(x1.list, place[j].list)){
						d = j;
						break;
					}
				}
				for (int j = 0; j < rec[d].size(); j++){
					if (0 == strcmp(x2.list, rec[d][j].list)){
						rec[d][j].val += x2.val;
						flag = 1;
						break;
					}
				}
				if (!flag) rec[d].push_back(x2);
			}
		}
		std::sort(place.begin(), place.end(), cmp);
		for (i = 0; i < k; i++){
			std::sort(rec[rt].begin(), rec[rt].end(), cmp);
			printf("%s\n", place[i].list);
			for (int j = 0; j < rec[rt].size(); j++){
				printf("   |----%s(%d)\n", rec[rt][j].list, rec[rt][j].val);
			}
			rec[rt].clear();
		}
		place.clear();
		if (t > 0) printf("\n");
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值