更新字典(Updating a Dictionary)

2023-05-16

Updating a Dictionary
Time Limit:1000MS Memory Limit:Unknown 64bit IO Format:%lld & %llu

Submit Status

Description

Download as PDF

In this problem, a dictionary is collection of key-value pairs, where keys are lower-case letters, and values are non-negative integers. Given an old dictionary and a new dictionary, find out what were changed.

Each dictionary is formatting as follows:

{key:value,key:value,...,key:value}

Each key is a string of lower-case letters, and each value is a non-negative integer without leading zeros or prefix `+'. (i.e. -4, 03 and +77 are illegal). Each key will appear at most once, but keys can appear in any order.

Input 

The first line contains the number of test cases T ( T$ \le$1000). Each test case contains two lines. The first line contains the old dictionary, and the second line contains the new dictionary. Each line will contain at most 100 characters and will not contain any whitespace characters. Both dictionaries could be empty.


WARNING: there are no restrictions on the lengths of each key and value in the dictionary. That means keys could be really long and values could be really large.

Output 

For each test case, print the changes, formatted as follows:
  • First, if there are any new keys, print `+' and then the new keys in increasing order (lexicographically), separated by commas.
  • Second, if there are any removed keys, print `-' and then the removed keys in increasing order (lexicographically), separated by commas.
  • Last, if there are any keys with changed value, print `*' and then these keys in increasing order (lexicographically), separated by commas.

If the two dictionaries are identical, print `No changes' (without quotes) instead.

Print a blank line after each test case.

Sample Input 


3
{a:3,b:4,c:10,f:6}
{a:3,c:5,d:10,ee:4}
{x:1,xyz:123456789123456789123456789}
{xyz:123456789123456789123456789,x:1}
{first:1,second:2,third:3}
{third:3,second:2}
  

Sample Output 


+d,ee
-b,f
*c

No changes

-first
  

【分析】

       用两个Map分别存储旧字典和新字典的键值,为了方便,同时使用2个链表对应存储键,这样遍历Map时就只需要用到这两个链表,用3个链表存储3种情况的值(新增键、删除键、修改键)。如果新字典的Map中有一个键不被旧字典的Map包含,则说明是新增键,并把该键存储在对应链表中;如果包含,还要判断对应是值是否相等,如果相等则不予以处理,如果不相等,则说明该键是修改键,并把该键存储在对应链表中;如果旧字典的Map中有一个键不被新字典的Map包含,则说明该键是删除键,并把该键存储在对应链表中;最后判断3个链表是否为空,如果为空,说明字典并没有发生改变。

【注意】

1、要注意输入的字典可能为空的情况,所以在处理字符串时,需要特别注意。否则结果可能因为访问越界容易出现Runtime error 的错误。

2、每个测试结果的最后均有一行空行。

3、输出结果需要按字典序从小到大排列,所以存储键的那两个链表在存储完所有键之后需要进行排序,那样从中取键时加入到结果链表中仍可以保持字典序,这样就可以不用对结果链表进行排序。

用java语言编写程序,代码如下:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.TreeMap;

public class Main {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int t = input.nextInt();
		
		input.nextLine();
		
		TreeMap<String, String> tm1 = new TreeMap<String, String>();
		TreeMap<String, String> tm2 = new TreeMap<String, String>();
		ArrayList<String> l1 = new ArrayList<String>();
		ArrayList<String> l2 = new ArrayList<String>();
		for(int i = 0; i < t; i++) {
			String s1 = input.nextLine();
			String s2 = input.nextLine();
			
			tm1.clear();
			tm2.clear();
			l1.clear();
			l2.clear();
			
			getValue(s1, tm1, l1);
			getValue(s2, tm2, l2);
			
			output(tm1, l1, tm2, l2);
		}
	}
	
	public static void getValue(String s, TreeMap<String, String> tm, ArrayList<String> list) {
		/*String[] sarr = s.substring(1, s.length() - 1).split(",");
		for(int j = 0; j < sarr.length; j++) {
			String[] stemp = sarr[j].split(":");
			tm.put(stemp[0], stemp[1]);
			list.add(stemp[0]);
		}*/
		int len = s.length();
		String strKey = "";
		String strValue = "";
		String temp = "";
		for(int i = 0; i < len; i++) {
			char c = s.charAt(i);
			if(c == ':') {
				if(!temp.equals("")) {
					strKey = "";
					strKey = "" + temp;
					list.add(strKey);
					temp = "";					
				}
			}
			else if(c == ',' || c == '}') {
				if(!temp.equals("")) {
					strValue = "";
					strValue = "" + temp;
					tm.put(strKey, strValue);
					//System.out.println(strKey + "...." + strValue);
					temp = "";					
				}
			}
			else if(c != '{' && c != '}') {
				temp = temp + c;
			}
		}
		Collections.sort(list);
	}
	
	public static void output(TreeMap<String, String> tm1, ArrayList<String> l1,
			TreeMap<String, String> tm2, ArrayList<String> l2) {		
		ArrayList<String> list1 = new ArrayList<String>();
		ArrayList<String> list2 = new ArrayList<String>();
		ArrayList<String> list3 = new ArrayList<String>();
		
		for(int i = 0; i < l2.size(); i++) {
			String key = l2.get(i);
			String value = tm2.get(key);
			
			if(tm1.containsKey(key)) {
				if(!tm1.get(key).equals(value))
					list3.add(key);
			}
			else {
				list1.add(key);
			}
		}
		
		for(int i = 0; i < l1.size(); i++) {
			String key = l1.get(i);
			if(!tm2.containsKey(key))
				list2.add(key);
		}
		
		if(list1.size() == 0 && list2.size() == 0 && list3.size() == 0)
			System.out.println("No changes");
		else {
			print('+', list1);
			print('-', list2);
			print('*', list3);			
		}
		System.out.println();
	}
	
	public static void print(char c, ArrayList<String> list) {
		if(list.size() != 0) {
			System.out.print(c + list.get(0));
			for(int i = 1; i < list.size(); i++)
				System.out.print("," + list.get(i));
			System.out.println();
		}
	}
}


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

更新字典(Updating a Dictionary) 的相关文章

随机推荐

  • 谜题(Puzzle)

    Puzzle Time limit 3 000 seconds Puzzle A children 39 s puzzle that was popular 30 years ago consisted of a 5x5 framewhic
  • 纵横字谜的答案(Crossword Answers)

    Crossword Answers Time limit 3 000 seconds Crossword Answers A crossword puzzle consists of a rectangular grid of black
  • DNA序列(DNA Consensus String)

    DNA Consensus String Time limit 3 000 seconds Figure 1 DNA Deoxyribonucleic Acid is the molecule which contains the gene
  • 古老的密码(Ancient Cipher)

    Ancient Cipher Time limit 3 000 seconds Ancient Roman empire had a strong governmentsystem with various departments incl
  • Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing

    原文转载自 sunny2038 的CSDN博客文章 原文地址 最近在看Java xff0c 在编译写书上一个例子时 xff0c 由于书上的代码只有一部分 xff0c 于是就自己补了一个内部类 结果编译时出现 xff1a No enclosi
  • 瑞星微开发工具下载镜像的配置方法?

    如何根据 parameter txt 建立配置表 xff1f 首先我们来看下 parameter txt 的内容 xff1a CMDLINE mtdparts 61 rk29xxnand 0x00002000 64 0x00004000 u
  • 特别困的学生(Extraordinarily Tired Students)

    Extraordinarily Tired Students Time limit 3 000 seconds When a student is too tired he can 39 t help sleeping in class e
  • 骰子涂色(Cube painting)

    Cube painting We have a machine for painting cubes It is supplied with three different colors blue red and green Each fa
  • 盒子(Box)

    Box Time limit 3 000 seconds Ivan works at a factory that produces heavy machinery He hasa simple job he knocks up woode
  • 循环小数(Repeating Decimals)

    Repeating Decimals Time limit 3 000 seconds Repeating Decimals The decimal expansion of the fraction 1 33 is wherethe is
  • 反片语(Ananagrams)

    反片语 Ananagrams 输入一些单词 xff0c 找出所有满足如下条件的单词 xff1a 该单词不能通过字母重排 xff0c 得到输入文本中的另外一个单词 在判断是否满足条件时 xff0c 字母不分大小写 xff0c 但在输出时应保留
  • 集合栈计算机(The SetStack Computer)

    The SetStack Computer Time limit 3 000 seconds 题目是这样的 xff1a 有一个专门为了集合运算而设计的 集合栈 计算机 该机器有一个初始为空的栈 xff0c 并且支持以下操作 xff1a PU
  • 代码对齐(Alignment of Code)

    Alignment of Code Time Limit 4000 2000 MS Java Others Memory Limit 32768 32768 K Java Others Total Submission s 958 Acce
  • Ducci序列(Ducci Sequence)

    Ducci Sequence Time limit 3 000 seconds A Ducci sequence is a sequence of n tuples of integers Given an n tuple of integ
  • 卡片游戏(Throwing cards away I)

    Problem B Throwing cards away I Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at
  • 交换学生(Foreign Exchange)

    Problem E Foreign Exchange Input standard input Output standard output Time Limit 1 second Your non profit organization
  • CAN通信物理容错测试checklist

    CAN通信物理容错测试checklist 工作项目中 xff0c 我们有时有些产品CAN总线功能 xff0c 一般情况下我们必须要满足以下几种状况的测试项才算符合要求 一 CAN H与CAN L短路 判定标准 xff1a 短接故障发生后 x
  • 对称轴(Symmetry)

    Symmetry Time limit 3 000 seconds The figure shown on the left is left right symmetric as it is possible to fold the she
  • 打印队列(Printer Queue)

    Printer Queue Time limit 3 000 seconds 分析 首先记录所求时间它在队列中的位置 xff0c 用一个队列存储这些任务的优先级 xff0c 同时也创建一个队列存储对应任务一开始的位置 xff0c 那么当我们
  • 更新字典(Updating a Dictionary)

    Updating a Dictionary Time Limit 1000MS Memory Limit Unknown 64bit IO Format lld amp llu Submit Status Description In th