湖南省第八届大学生计算机程序设计竞赛C题 Updating a Dictionary
来源:互联网 发布:美橙域名证书下载 编辑:程序博客网 时间:2024/05/16 01:19
湖南省第八届大学生计算机程序设计竞赛C题 Updating a Dictionary(题目链接)。
Problem C 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 ( T≤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和set都会按照operator<自动排序,正好string类有operator<函数,因此就用这两个。map映射一个key,value对。把旧字典的数据插入这个map中。新字典与它比较,如果没找到,添加到add集合中。如果找到了则删除,删除前判断value值是否相同,如果不同,就插入change集合。如此一来,没删除的就是"-"的元素。然后先后输出add集合、"-"的元素和change集合的元素即可。C++语言代码如下:
#include <cstdio>#include <cstdlib>#include <string>#include <cctype>#include <map>#include <set>using namespace std;#define MAX_LENGTH 200int main ( ){ int test_cases; char line[MAX_LENGTH]; string key, value; map <string, string> dic; set <string> add, change; scanf( "%d", &test_cases ); gets( line ); while ( test_cases -- ) { key = value = ""; dic.clear( ); gets( line ); for ( int i = 1 ; line[i] != '\0' ; i ++ ) { if ( isdigit(line[i]) ) value += line[i]; else if ( isalpha(line[i]) ) key += line[i]; else if ( line[i] == ',' ) { dic.insert( make_pair<string, string>(key, value) ); key = value = ""; } else if ( line[i] == '}' ) { if ( key != "" ) dic.insert( make_pair<string, string>(key, value) ); key = value = ""; } } add.clear( ); change.clear( ); gets( line ); for ( int i = 1 ; line[i] != '\0' ; i ++ ) { if ( isdigit(line[i]) ) value += line[i]; else if ( isalpha(line[i]) ) key += line[i]; else if ( line[i] == ',' ) { map <string, string> :: iterator it = dic.find( key ); if ( it == dic.end( ) ) add.insert( key ); else { if ( it->second != value ) change.insert( key ); dic.erase( it ); } key = value = ""; } else if ( line[i] == '}' ) { if ( key != "" ) { map <string, string> :: iterator it = dic.find( key ); if ( it == dic.end( ) ) add.insert( key ); else { if ( it->second != value ) change.insert( key ); dic.erase( it ); } key = value = ""; } } } if ( dic.size( ) == 0 && add.size( ) == 0 && change.size( ) == 0 ) puts( "No changes" ); else { if ( add.size( ) != 0 ) { set <string> :: const_iterator it; it = add.begin( ); printf( "+%s", it->c_str( ) ); for ( ++ it ; it != add.end( ) ; ++ it ) printf( ",%s", it->c_str( ) ); puts( "" ); } if ( dic.size( ) != 0 ) { map <string, string> :: const_iterator it; it = dic.begin( ); printf( "-%s", it->first.c_str( ) ); for ( ++ it ; it != dic.end( ) ; ++ it ) printf( ",%s", it->first.c_str( ) ); puts( "" ); } if ( change.size( ) != 0 ) { set <string> :: const_iterator it; it = change.begin( ); printf( "*%s", it->c_str( ) ); for ( ++ it ; it != change.end( ) ; ++ it ) printf( ",%s", it->c_str( ) ); puts( "" ); } } puts( "" ); } return EXIT_SUCCESS;}
- 湖南省第八届大学生计算机程序设计竞赛C题 Updating a Dictionary
- CSU 1113 Updating a Dictionary (湖南省第八届大学生计算机程序设计竞赛)
- 湖南生第八届大学生程序设计大赛原题 C-Updating a Dictionary(UVA12504 - Updating a Dictionary)
- 湖南省第八届大学生计算机程序设计竞赛C题(UVA 12504)
- 病毒(湖南省第八届大学生计算机程序设计竞赛)
- 湖南省第八届大学生计算机程序设计竞赛部分题解
- 湖南省第十二届大学生计算机程序设计竞赛 A 2016
- 湖南省第九届大学生计算机程序设计竞赛A
- 2012年湖南省第八届大学生计算机程序设计竞赛 F 题 Kingdoms
- 湖南省第八届大学生计算机程序设计竞赛D题 平方根大搜索
- 湖南省第十一届大学生计算机程序设计竞赛 A,B,C,D,F,H,J 题解
- 湖南省第九届大学生计算机程序设计竞赛
- 湖南省第七届大学生计算机程序设计竞赛
- CSU 1803 2016【湖南省第十二届大学生计算机程序设计竞赛 A题】
- 湖南省第十届大学生计算机程序设计竞赛(原题) 第c题 酷酷的单词
- 湖南省第八届省赛 Updating a Dictionary (模拟)
- 湖南省第八届程序设计竞赛 A
- 最短的名字(湖南省第八届大学生计算机程序设计竞赛)
- C++ 11 STL 线程库实现的线程同步与互斥
- C++11标准 STL正则表达式 验证电子邮件地址
- POSIX正则表达式 验证电子邮件地址
- 湖南省第八届大学生计算机程序设计竞赛D题 平方根大搜索
- boost正则表达式 验证电子邮件地址
- 湖南省第八届大学生计算机程序设计竞赛C题 Updating a Dictionary
- JavaScript正则表达式 验证电子邮件地址
- PHP正则表达式 验证电子邮件地址
- 简易的个人信息验证网页
- C++ 11 标准 Lambda表达式
- C++ 11 标准 新增的Lambda表达式、for_each语法,改变了auto关键字的意义
- C++11标准新增可变参数模板(variadic template)
- base64编码、解码的C语言实现
- 有一种深爱,叫做不再联系