习题5 - 11 更新字典(Updating a Dictionary, UVa12504)

本文解析了UVa12504习题“更新字典”的实现思路与代码细节,通过对比两个字典找出新增、删除及更改的条目,并给出具体的输出格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#ifdef _DEBUG
#pragma warning(disable : 4996)
#endif
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <sstream>
#include <utility>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cctype>
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define CLOSE() ios::sync_with_stdio(false)
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
#define PF(a) printf("%d\n", a)
#define SF(a) scanf("%d", &a)
#define SFF(a, b) scanf("%d%d", &a, &b)
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define LL long long
#define MAXN 100005
#define MAXM 1000010
#define mod 1000007
#define INF 1000000000
using namespace std;
//------------------------------------------------------------------------------------------//
//习题5 - 11 更新字典(Updating a Dictionary, UVa12504)
//注意值为空的情况
//substr 第二个参数为子串的长度
map<string, string> dic, dic2;
set<string> add, decrease, change;

void solve() {
	for (auto i : dic) {
		string k = i.first, v = i.second;
		if (!dic2.count(k)) decrease.insert(k);
		else {
			if (v != dic2[k]) change.insert(k);
		}
	}
	for (auto i : dic2) {
		string k = i.first, v = i.second;
		if (!dic.count(k)) add.insert(k);
	}
}

int main() {
#ifdef _DEBUG
	IN(); OUT();
#endif
	int T; SF(T);
	while (T--) {
		dic.clear();
		add.clear();
		decrease.clear();
		change.clear();
		dic2.clear();
		string s;
		cin >> s;
		bool ok = false;
		int p0 = 1, p1 = s.find(':'), p2 = s.find(','), p3 = s.find('}');
		if (p2 == -1) p2 = p3, ok = true;
		if (p1 == -1) goto here;
		while (1) {
			string k = s.substr(p0, p1-p0), v = s.substr(p1 + 1, p2-p1-1);
			//cout << k << v << endl;
			dic[k] = v;
			if (ok) break;
			p0 = p2 + 1;
			p1 = s.find(':', p0);
			p2 = s.find(',', p0);
			if (p2 == -1) p2 = p3, ok = true;
		}
		here:
		cin >> s;
		ok = false;
		p0 = 1, p1 = s.find(':'), p2 = s.find(','), p3 = s.find('}');
		if (p1 == -1) goto there;
		if (p2 == -1) p2 = p3, ok = true;
		while (1) {
			string k = s.substr(p0, p1 - p0), v = s.substr(p1 + 1, p2 - p1 - 1);
			//cout << k << v << endl;
			dic2[k] = v;
			if (ok) break;
			p0 = p2 + 1;
			p1 = s.find(':', p0);
			p2 = s.find(',', p0);
			if (p2 == -1) p2 = p3, ok = true;
		}
		there :
		solve();
		bool have = false;
		if (add.size()) {
			have = true;
			cout << '+';
			bool first = true;
			for (auto i : add) {
				if (first) first = false;
				else cout << ',';
				cout << i;
			}
			cout << endl;
		}
		if (decrease.size()) {
			have = true;
			cout << '-';
			bool first = true;
			for (auto i : decrease) {
				if (first) first = false;
				else cout << ',';
				cout << i;
			}
			cout << endl;
		}
		if (change.size()) {
			have = true;
			cout << '*';
			bool first = true;
			for (auto i : change) {
				if (first) first = false;
				else cout << ',';
				cout << i;
			}
			cout << endl;
		}
		if (!have) cout << "No changes\n";
		cout << endl;
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值