UVA - 12504

 Updating a Dictionary 

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 ( T1000). 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

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <map>
 5
 6 using namespace std;
 7
 8 map <string,string> ValueCatchOne;
 9 map <string,string> ValueCatchTwo;
10
11 void Work(map<string,string>& IDcatch,string str) {
12     int ok = 0;
13     string key = "",value = "";
14     for (int i = 1,len = str.length();i < len;i++) {
15         if (ok == 0) {
16             if (str[i] == ‘:‘) {
17                 ok = 1;
18                 continue;
19             }
20             key += str[i];
21         }
22         else {
23             if (str[i] != ‘,‘ && str[i] != ‘}‘) value += str[i];
24             else {
25                 ok = 0;
26                 IDcatch[key] = value;
27                 key = "";value = "";
28             }
29         }
30     }
31 }
32
33 int main () {
34     int T;
35     // freopen("1.in","r",stdin);
36     cin >> T;
37     getchar();
38     while (T--) {
39         string str;
40         ValueCatchOne.clear();
41         ValueCatchTwo.clear();
42         cin >> str;
43         Work(ValueCatchOne,str);
44         cin >> str;
45         Work(ValueCatchTwo,str);
46         if (ValueCatchOne.size() == ValueCatchTwo.size()) {
47             int ok = 1;
48             for (map<string,string>::iterator Iter = ValueCatchOne.begin();Iter != ValueCatchOne.end();Iter++) {
49                 if (ValueCatchTwo.count(Iter -> first) == 0 || ValueCatchTwo[Iter->first] != Iter -> second) {
50                     ok = 0;
51                     break;
52                 }
53             }
54             if (ok == 1) {
55                 cout << "No changes" << endl;
56                 cout << endl;
57                 continue;
58             }
59         }
60         {
61             int num = 0;
62             for (map<string,string>::iterator Iter = ValueCatchTwo.begin();Iter != ValueCatchTwo.end();Iter++) {
63                 if (ValueCatchOne.count(Iter -> first) == 0) {
64                     cout << (!num?‘+‘:‘,‘) << Iter -> first;
65                     num++;
66                 }
67             }
68             if (num) cout << endl;
69             num = 0;
70             for (map<string,string>::iterator Iter = ValueCatchOne.begin();Iter != ValueCatchOne.end();Iter++) {
71                 if (ValueCatchTwo.count(Iter -> first) == 0) {
72                     cout << (!num?‘-‘:‘,‘) << Iter -> first;
73                     num++;
74                 }
75             }
76             if (num) cout << endl;
77             num = 0;
78             for (map<string,string>::iterator Iter = ValueCatchOne.begin();Iter != ValueCatchOne.end();Iter++) {
79                 if (ValueCatchTwo.count(Iter -> first) && ValueCatchTwo[Iter -> first] != Iter -> second) {
80                     cout << (!num?‘*‘:‘,‘) << Iter -> first;
81                     num++;
82                 }
83             }
84             if (num) cout << endl;
85         }
86         cout << endl;
87     }
88 }

时间: 2024-10-12 00:06:10

UVA - 12504的相关文章

UVa 12504 Updating a Dictionary(更新字典)

题意  比较两个字典  按字典序输出所有添加 删除 修改的项   如果没有任何更新  输出  No changes STL map的应用  对比两个字典  注意开始字符串的处理和字典可以为空 #include<bits/stdc++.h> using namespace std; map<string, string> d[2]; map<string, string>::iterator it; const int N = 105; string s, a, b, t

UVA 12504 Updating a Dictionary

题目链接:https://vjudge.net/problem/UVA-12504 题目翻译摘自<算法禁赛入门经典> 题目大意 在本题中,字典是若干键值对,其中键为小写字母组成的字符串,值为没有前导零或正号的非负整数(-4,03 和 +77 都是非法的,注意该整数可以很大).输入一个旧字典和一个新字典,计算二者的变化.输入的两个字典中键都是唯一的,但是排列顺序任意. 输入包含两行,各包含不超过100个字符,即旧字典和新字典.输出格式如下: 如果至少有一个新增键,打印一个“+”号,然后是所有新增

第五章-習題(1-11)待續

5-1 代碼對齊(UVa 1593) 不難,按行讀取,然後stringstream輸入到vector<string>那裏去,算出行最大單詞數,再算出列單詞最大寬度,然後就可以格式化輸出了: #include<iostream> #include<string> #include<algorithm> #include<vector> #include<cstdio> #include<sstream> using name

【UVA】12504 - Updating a Dictionary(map,string,vector模拟)

一般的模拟题,一开始WA,可能是用string的容器放char,改成string就过了 14073581 12504 Updating a Dictionary Accepted C++ 0.032 2014-08-21 07:12:19 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> #include<st

【UVA】12504 Updating a Dictionary(STL)

题目 题目 ? ? 分析 第一次用stringstream,真TMD的好用 ? ? 代码 #include <bits/stdc++.h> using namespace std; int main() { int n; cin>>n; getchar();//回车 while(n--) { string s1,s2; getline(cin,s1); getline(cin,s2); for(int i=0;i<s1.length();i++) if(!isalpha(s1

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 10341 Solve It

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and te

UVA 11014 - Make a Crystal(容斥原理)

UVA 11014 - Make a Crystal 题目链接 题意:给定一个NxNxN的正方体,求出最多能选几个整数点.使得随意两点PQ不会使PQO共线. 思路:利用容斥原理,设f(k)为点(x, y, z)三点都为k的倍数的点的个数(要扣掉一个原点O).那么全部点就是f(1),之后要去除掉共线的,就是扣掉f(2), f(3), f(5)..f(n).n为素数.由于这些素数中包括了合数的情况,而且这些点必定与f(1)除去这些点以外的点共线,所以扣掉.可是扣掉后会扣掉一些反复的.比方f(6)在f

[UVa] Palindromes(401)

UVA - 401 Palindromes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDED