【习题 5-11 UVA 12504 】Updating a Dictionary

【链接】 我是链接,点我呀:)
【题意】

在这里输入题意

【题解】

不确定某个map里面是否有某个关键字的时候。 要用find来确定。 如果直接用访问下标的形式去做的话。 会强行给他加一个那个关键字(原来没有). (当然那个关键字的映射为空就是了);

【代码】

#include <bits/stdc++.h>
using namespace std;

string s;
map<string, string> mmap1, mmap2;
vector <string> v[3];

void getmap(map<string, string> &mmap, string s)
{
	int len = s.size();
	for (int i = 0; i < len; i++)
		if (s[i] == '{' || s[i] == ':' || s[i] == ',' || s[i] == '}')
			s[i] = ' ';
	string a, b;
	stringstream ss(s);
	while (ss >> a >> b)
	{
		mmap[a] = b;
	}
}

int main()
{
	//freopen("F:\rush.txt", "r", stdin);
	int unused;
	cin >> unused;
	while (unused--)
	{
		for (int i = 0; i < 3; i++)v[i].clear();
		mmap1.clear(); mmap2.clear();
		cin >> s;
		getmap(mmap1, s);
		cin >> s;
		getmap(mmap2, s);
		for (auto temp : mmap2)
			if (mmap1.find(temp.first)==mmap1.end()) v[0].push_back(temp.first);

		for (auto temp : mmap1)
			if (mmap2.find(temp.first)==mmap2.end()) v[1].push_back(temp.first);

		for (auto temp : mmap1)
			if (mmap2.find(temp.first)!=mmap2.end() && mmap2[temp.first] != temp.second)
			{
				string s1 = temp.second, s2 = mmap2[temp.first];
				v[2].push_back(temp.first);
			}
		bool ok = v[0].empty() && v[1].empty() && v[2].empty();
		if (ok) cout << "No changes" << endl;
		else
		{
			for (int i = 0; i < 3; i++)
				if (!v[i].empty())
				{
					if (i == 0) cout << '+';
					if (i == 1) cout << '-';
					if (i == 2) cout << '*';
					cout << v[i][0];
					for (int j = 1; j < (int)v[i].size(); j++)
						cout << "," << v[i][j];
					cout << endl;
				}
		}

		cout << endl;
	}
	return 0;
}
原文地址:https://www.cnblogs.com/AWCXV/p/7684988.html