UVA - 12504 Updating a Dictionary

本文详细解析了UVA-12504题目的解决方案,通过使用C++的STL库中的map数据结构来对比两个字典,找出新增、删除和更改的条目。代码中包含了完整的实现过程,包括输入处理、键值对读取和比较逻辑。

传送门:UVA-12504

相比于前两个STL的题,这题水多了。。。

AC代码:
 

#include<iostream>
#include<map>
#include<vector>
#include<sstream>
using namespace std;
int main()
{
	int n;
	cin>>n;
	while(n--){
		string tmp1,tmp2,f1,f2;
		cin>>tmp1>>tmp2;
		map<string,string> p1,p2;//存两个字典 
		//将键值对用空格分隔 
		for(int i=0;i<tmp1.size();++i) if(!(isalpha(tmp1[i])||tmp1[i]>='0'&&tmp1[i]<='9')) tmp1[i]=' ';
		for(int i=0;i<tmp2.size();++i) if(!(isalpha(tmp2[i])||tmp2[i]>='0'&&tmp2[i]<='9')) tmp2[i]=' ';
		stringstream ss1(tmp1),ss2(tmp2);
		while(ss1>>f1>>f2) p1[f1]=f2;
		while(ss2>>f1>>f2) p2[f1]=f2;
		int cnt1=0,cnt2=0,cnt3=0;//分别代表三种情况的个数 
		for(map<string,string>::iterator it=p2.begin();it!=p2.end();++it)
			if(!p1.count(it->first)){
				if(!(cnt1++)) cout<<"+"<<it->first;
				else cout<<","<<it->first;
			}
		if(cnt1) cout<<endl;
		for(map<string,string>::iterator it=p1.begin();it!=p1.end();++it)
			if(!p2.count(it->first)){
				if(!(cnt2++)) cout<<"-"<<it->first;
				else cout<<","<<it->first;
	    	}
	    if(cnt2) cout<<endl;
	    for(map<string,string>::iterator it=p2.begin();it!=p2.end();++it)
	    	if(p1.count(it->first)&&p1[it->first]!=it->second){
	    		if(!(cnt3++)) cout<<"*"<<it->first;
	    		else cout<<","<<it->first;
			}
		if(cnt3) cout<<endl;
		if(!cnt1&&!cnt2&&!cnt3) cout<<"No changes\n";
		cout<<endl;
	}
	return 0;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值