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[N];

void print(char c, int n)
{
    sort(t, t + n), cout << c << t[0];
    for(int i = 1; i < n; ++i) cout << ',' << t[i];
    puts("");
}

int main()
{
    int cas, n, c1, c2, c3;
    cin >> cas;
    while(cas--)
    {
        d[0].clear(), d[1].clear();
        for(int i = 0; i < 2; ++i)
        {
            cin >> s;
            int j = 1, l = s.size();
            while(l > 2 && j < l)
            {
                while(s[j] != ':') a += s[j++]; ++j;
                while(s[j] != ',' && s[j] != '}') b += s[j++]; ++j;
                d[i][a] = b, a = b = "";
                //cout << a << " : " << b << endl;
            }
        }

        c1 = c2 = c3 = 0;
        for(it = d[1].begin(); it != d[1].end(); ++it)
            if(!d[0].count(it->first)) t[c1++] = it->first;
        if(c1) print('+', c1);

        for(it = d[0].begin(); it != d[0].end(); ++it)
            if(!d[1].count(it->first)) t[c2++] = it->first;
        if(c2) print('-', c2);

        for(it = d[1].begin(); it != d[1].end(); ++it)
            if(d[0].count(it->first) && d[0][it->first] != it->second) t[c3++] = it->first;
        if(c3) print('*', c3);

        if(!(c1 || c2 || c3)) puts("No changes");
        puts("");
    }
    return 0;
}

 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

时间: 2024-11-06 03:30:06

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

UVA 12504 Updating a Dictionary

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

【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 511 Updating a Dictionary

大致题意:用{ key:value, key:value, key:value }的形式表示一个字典key表示建,在一个字典内没有重复,value则可能重复 题目输入两个字典,如{a:3,b:4,c:10,f:16}  {a:3,c:5,d:10,ee:4} 于新字典相比,找出旧字典的不同,并且输出用相应格式,增加的用'+',如 +d,ee 减少的用'-',如 -b,f value变化的用*,如 *c 没有不同则输出"No changes"" 如果使用java,则可以用Tre

Uva 10815-Andy&#39;s First Dictionary(串)

Problem B: Andy's First Dictionary Time limit: 3 seconds Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him, as the number of words that he knows is, well, not quite enough. Instead of thinking up all

Dictionary(字典)

Dictionary(字典) 字典是python中唯一的映射类型,采用键值对(key-value)的形式存储数据.python对key进行哈希函数运算,根据计算的结果决定value的存储地址,所以字典是无序存储的,且key必须是可哈希的.可哈希表示key必须是不可变类型,如:数字.字符串.元组. 字典(dictionary)是除列表意外python之中最灵活的内置数据结构类型.列表是有序的对象结合,字典是无序的对象集合.两者之间的区别在于:字典当中的元素是通过键来存取的,而不是通过偏移存取. 创

uva 11488 - Hyper Prefix Sets(字典树)

H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of strings in the set. For example the prefix goodness of the set {000,001,0011} is 6.You are given a set of binary strings. Find the maximum prefix goodnes

UVA 10815-Andy&#39;s First Dictionary(字符串模拟+排序+重复删除)

Andy's First Dictionary Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description Problem B: Andy's First Dictionary Time limit: 3 seconds Andy, 8, has a dream - he wants to produce his very own dictionary. This

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