习题5_11 uva12504更新字典

Print a blank line after each test case.
emmm被坑了好多次,这句话我现在看懂啦嘿嘿嘿~
题目问的是对于两个新旧字典,比较他们的增删改变情况,只要会重载,那都不是事,
注意set 的迭代器是木有修改功能的! 只有增删,那咋整?先删后加呗。
思路如下:
先对旧字典进行遍历,忽然发现新的没有,说明什么?删了呗
发现有,比较一下下是否num 一样,不一样?改了呗 ,然后这里我把发现有的数在新字典上做了标记,
有啥用?再对新字典进行遍历!发现莫得标记,那是啥?增的呗! 那就都ok啦!
然后如果发现三个字典为空,那么就是一成不变,否则就输出呗
至于刚开始的读,找找规律吧----
上代码

#include <bits/stdc++.h>
using namespace std;
#define Set set<stu>:: iterator
struct stu{
	string s;
	string num;
	int f = 0;
	bool operator < (const stu b) const{
	 return   this->s < b.s; //set按字典序排序用 
	} 
	bool operator == (const stu b) const{
		return this->s == b.s;
	}  //如果字符串一样,认为一样!好判断是改还是不变 
}q,q1,q2;
void de_str(set<stu> &W , string ss)
{
	int n = ss.size(),be;
	for (int i = 1; i < n;)
	{
		if(ss[i] =='}') break;
	    be = i;
		while(isalpha(ss[i])) i++;
		q.s = ss.substr(be,i-be);
		i++; 
		be = i;
		while(isdigit(ss[i])) i++;
		q.num = ss.substr(be,i-be);
		W.insert(q);
		i++; 
	}
}  // 
void print(set<string> w,int i)
{
	for (set<string>:: iterator it = w.begin(); it != w.end(); it++)
	{
		if(it == w.begin())
		{
			if(!i)		  printf("+");
			else if(i==1) printf("-");
			else 		  printf("*");
		}
		else    		  printf(",");
		cout << *it;
	}
	if(!w.empty()) puts("");
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		string s1,s2;
		cin>>s1>>s2;
		set<stu> A,B;
		set<string> add,left,change;
		de_str(A , s1);
		de_str(B , s2);
		for (Set  it = A.begin(); it != A.end(); it++)
		{
				q1 = *it;
				if(B.find(*it) == B.end()) left.insert(q1.s); //被删 
				else{
					  Set it2 = B.find(*it); q2 = *it2;
					  if(q1.num != q2.num) change.insert(q1.s);
					  q2.f = 1;  B.erase(it2);  B.insert(q2);  
				}
		} //找删和改 
		for (Set it = B.begin(); it != B.end();  it++)
		{
			if((*it).f == 0) add.insert((*it).s);
		} //找增 
		if(left.empty() && add.empty() && change.empty()) puts("No changes");
		else
		{
			print(add,0);
			print(left,1);
			print(change,2);
		}
		printf("\n");
	}
	return 0;
} 
发布了55 篇原创文章 · 获赞 1 · 访问量 2651

猜你喜欢

转载自blog.csdn.net/qq_37548017/article/details/100524473
今日推荐