UVA - 12504 Updating a Dictionary

/*
  法一借鉴自:
  http://blog.csdn.net/wcr1996/article/details/41050253
  
  总结:
  1.
  写一个IsValid()函数,是十分必要的。因为这题有一个潜在的坑点,就是 key:value 这个对应关系中,key可能为空,value也可能为空,也有可能两者都为空
  
  这个坑点是WA以后,看到这个博主的博客 
  http://www.cnblogs.com/keanuyaoo/p/3343513.html
  发现的,我觉得很有道理,因为以上三种情况,讲道理都是不该建立 key-value 对,插入map和set的,毕竟它们是残缺的配对,所以,必须单独提出来考虑这三种情况
  
  在这个方法中,我的处理就是加入一个函数,这样比较简洁清晰,当然也有别的处理方法
  
  2.
  string的清空,可以用string的clear函数;也能用别的方式清空,我用的是写一个函数,在函数里将两个数据成员直接赋空串(其实在函数里调string成员的clear()函数,当然也可以);
  而原博主,是定义多一个string,也就是y,没有初始化时,C++默认y是空串,后面再用y来赋值x,来达到清空string的作用
  
  3. 
  注意题目中的"Print a blank line after each test case."注意是each,包括最后一组
  
  哪怕是数据格式控制这样的小细节,也不能想当然,要仔细读题,这题对最后一组的格式控制,就和以往不同
  
  4.
  该方法用到了一个小技巧:在处理新旧字典的输入时,将所有出现过的key存入集合中,等到最后进行处理,判断键值是否新增、减少、改变
  
  而如果没有定义 key 这个集合,输入第二个词典时,要一一检查有没有第一个中没出现过的,或者出现过但不相同的
  
  并且,之后还要重新循环一遍第一个词典,看看有没有第一个出现,但第二个没出现的key,我觉得比起用set,多费了一些功夫
  (可以参考看看“这个博客”,如果不用set存两个词典的key的并集,在找前一字典出点,后一字典未出现的键时,就需要输完后一字典以后,重新来遍历一次前一词典,就显得比较麻烦了)
  “这个博客”: http://blog.csdn.net/u014360070/article/details/52133383
*/


#include <iostream>
#include <string>
#include <map>
#include <set> 
#include <vector>
using namespace std;

struct key_value
{
	string key, value;
	void ReClear()
    {
    	key = value = "";
	}
	bool IsVailid()
	{
		return (key != "" && value != "");
	}
};
set<string> key;
vector<string> change[3];
map<string, string> dic[2]; //dictionary

void ReInit()
{
	for (int i = 0; i < 2; i++) dic[i].clear();
	for (int i = 0; i < 3; i++) change[i].clear();
	change[0].push_back("+");
	change[1].push_back("-");
	change[2].push_back("*"); //初始化三种改变对应的符号输出
	
	key.clear();
}

int main()
{
	int t, add_s = 1;
	key_value x;
	cin >> t;
	getchar();
	while (t--)
	{
		ReInit();
		char c;
		
		for (int i = 0; i < 2; )
		{
			c = getchar();
			if (c == '{')
			{
				add_s = 1;
				x.ReClear();			
			}
			else if (c == '}')
			{
				if (x.IsVailid())
				{
					dic[i][x.key] = x.value;
					key.insert(x.key);
				}
				i++;
				getchar(); //处理空格	
			}
			else if (c == ':')
			{
				add_s = !add_s;
			}
			else if (c == ',')
			{
				add_s = !add_s;
				if (x.IsVailid())
				{
					dic[i][x.key] = x.value;
					key.insert(x.key);
					x.ReClear();
				} 
			}
			else
			{
				if (add_s) x.key += c;
				else x.value += c;
			}
		}
		
		for (set<string>::iterator it = key.begin(); it != key.end(); it++)
		{
			if (!dic[0].count(*it)) change[0].push_back(*it);
			else if (!dic[1].count(*it)) change[1].push_back(*it);
			else if (dic[0][*it] != dic[1][*it]) change[2].push_back(*it);
		} //找出所有的改动 
		
		int check = 0;
		for (int i = 0; i < 3; i++)
		{
			int first = 2;
			if (change[i].size() > 1) //如果只有一个,说明存的是符号 
			{
				for (int j = 0; j < change[i].size(); j++)
				{
					if (first) first--;
					else cout << ",";
					cout << change[i][j];
				}
				cout << endl;
				check++;
			}	
		}
		if (!check) cout << "No changes" << endl;
		cout << endl;
	}
	return 0;
}


/*
  对法一改写,思路借鉴自:
  http://blog.csdn.net/u014360070/article/details/52133383
  
  改进:
  1. 对于字典读入方面,有改进,主要体现在:
	 1.1 用string而不是用getchar读,并且,按照题意"键为小写字母组成的字符串","值为没有前导零或正号的非负整数",使得 字符c 究竟加到 key 还是 value这个问题,不再需要引入 add_s 变量,只需要判断是字母还是数字,就可以直接判断了
	 
  我觉得可该进的小细节:
  1. 用一个set来存两个字典的并集,原因上文有讲到
  此外,用set还有一个额外的很大的好处,就是,set内部,是按照key的大小来排序的,所以我在将有变化的key,压入change[0/1/2]时,也一定是按字典序压入的,就不需要再去调用sort
  
  2. 对第一个词典的第二次遍历(找被删除的key时)的代码片段,可改写为下面的代码,利用map的迭代器完成,就不用再将key重新拼接一次了
  
  for (map<string,string>::iterator it = pq.begin(); it != pq.end(); it++)
        {
        	if (!((it->first).empty()) && !qp.count(it->first))
        	{
        		f2 = 1;
        		s5[k2++] = it->first;
			}
		}
*/

#include <iostream>
#include <string>
#include <map>
#include <set> 
#include <vector>
using namespace std;

struct key_value
{
	string key, value;
	void ReClear()
    {
    	key.clear();
    	value.clear();
	}
	bool IsVailid()
	{
		return (key != "" && value != "");
	}
};
set<string> key;
vector<string> change[3];
map<string, string> dic[2]; //dictionary

void ReInit()
{
	for (int i = 0; i < 2; i++) dic[i].clear();
	for (int i = 0; i < 3; i++) change[i].clear();
	change[0].push_back("+");
	change[1].push_back("-");
	change[2].push_back("*"); //初始化三种改变对应的符号输出
	
	key.clear();
}

int main()
{
	int t;
	key_value x;
	cin >> t;
	getchar();
	while (t--)
	{
		ReInit();
		string str;
		char c;
		
		for (int i = 0; i < 2; )
		{
			getline(cin, str);
			for (int j = 0; j < str.size(); j++)
			{
				c = str[j];
				if (c == '{')
				{
					x.ReClear();
				}
				else if (c == '}')
				{
					if (x.IsVailid())
					{
						dic[i][x.key] = x.value;
						key.insert(x.key);
					}
					i++;
			//		getchar(); //处理空格	
				}
				else if (c == ',')
				{
					if (x.IsVailid())
					{
						dic[i][x.key] = x.value;
						key.insert(x.key);
						x.ReClear();
					} 
				}
				else 
				{
					if (isalpha(c)) x.key += c;
					else if(isdigit(c)) x.value+= c;
				}
			}
		}
			
		
		for (set<string>::iterator it = key.begin(); it != key.end(); it++)
		{
			if (!dic[0].count(*it)) change[0].push_back(*it);
			else if (!dic[1].count(*it)) change[1].push_back(*it);
			else if (dic[0][*it] != dic[1][*it]) change[2].push_back(*it);
		} //找出所有的改动 
		
		int check = 0;
		for (int i = 0; i < 3; i++)
		{
			int first = 2;
			if (change[i].size() > 1) //如果只有一个,说明存的是符号 
			{
				for (int j = 0; j < change[i].size(); j++)
				{
					if (first) first--;
					else cout << ",";
					cout << change[i][j];
				}
				cout << endl;
				check++;
			}	
		}
		if (!check) cout << "No changes" << endl;
		cout << endl;
	}
	return 0;
}

/*
  法三收获(这个方法我没有改写,只记下学到了什么新用法)
  法三链接: http://blog.csdn.net/chy20142109/article/details/50651796
  
  1. 关于 strchr 函数
  http://blog.csdn.net/tommy_wxie/article/details/7554263
  
  函数原型:extern char *strchr(char *str,char character)
  参数说明:str为一个字符串的指针,character为一个待查找字符。
        
  所在库名:#include <string.h>
  
  函数功能:从字符串str中寻找字符character第一次出现的位置。
  
  返回说明:返回指向第一次出现字符character位置的指针,如果没找到则返回NULL。
  其它说明:还有一种格式char *strchr( const char *string, int c ),这里字符串是以int型给出的。
  
  2. 关于string的构造方法:
  string(t,k),其中t和k都为 char* 类型,则string中的值,为地址范围[t, k)范围内的值
  
  测试代码见下:(可以帮助更好地理解string的这种构造方式)
  #include <iostream>
  #include <cstring>
  #include <string>
  using namespace std;

  int main()
  {
	  char temp[109] = "There.I.would.like.to.have.a.test.";
	  char*a = temp, *b = temp + 6;
	  string t = string(a, b);
	  cout << t << endl;
      return 0;
  }
*/





posted @ 2017-09-15 20:11  mofushaohua  阅读(149)  评论(0编辑  收藏  举报