csuoj 1113: Updating a Dictionary

http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1113

1113: Updating a Dictionary

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 491  Solved: 121
[Submit][Status][Web Board]

Description

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

HINT

Source

湖南省第八届大学生计算机程序设计竞赛

分析:

STL容器的使用。

AC代码:

 #include<cstring>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<string>
#include <cctype>
#include<map>
using namespace std;
char s1[];
char s2[];
char s3[];
char s4[];
bool cmp(string ss,string sss)
{
return ss<sss;
}
string st,ed;
int main()
{
int T,t1,t2;
scanf("%d",&T);
gets(s1);
while(T--)
{ map<string,string>ss1;
map<string,string>ss2;
map<string,string>::iterator it;
string ss[];
string plu[],dir[],key[];
int k1 = ,k2 =,k3 =,k4=;
ss1.clear();
ss2.clear();
for(int i=;i<;i++)
{
ss[i].clear();
plu[i].clear();
dir[i].clear();
key[i].clear();
}
gets(s1);
gets(s2);
st = ed = "";
int len1 = strlen(s1);
int len2 = strlen(s2);
for(int i=;i<len1;i++)
{
if(isdigit(s1[i]))
ed = ed+s1[i];
else if(isalpha(s1[i]))
st = st+s1[i];
else if(s1[i] == ',')
{
ss1[st] =ed;
ss[k4++] = st;
st = ed = "";
}
else if(s1[i] == '}')
{
if(st!="")
{
ss[k4++] = st;
ss1[st] =ed;
}
st = ed = "";
}
}
for(int i=;i<len2;i++)
{
if(isdigit(s2[i]))
ed =ed+s2[i];
else if(isalpha(s2[i]))
st = st+s2[i];
else if(s2[i] == ',')
{
it = ss1.find(st);
if(it!=ss1.end())//如果找到
{
if(ss1[st] != ed)//增加的
key[k3++] = st;
}
else
{
plu[k1++] = st;
}
ss2[st] =ed;
st = ed = "";
}
else if(s2[i] == '}')
{
if(st!="")
{
it = ss1.find(st);
if(it!=ss1.end())//如果找到
{
if(ss1[st] != ed)//增加的
key[k3++] = st;
}
else
{
plu[k1++] = st;
}
ss2[st] =ed;
}
st = ed = "";
}
}
for(int i=;i<k4;i++)
{
it = ss2.find(ss[i]);
if(it == ss2.end())//如果没有找到
{
dir[k2++] = ss[i];
}
}
if(k1+k2+k3 == )
printf("No changes\n");
else
{
sort(plu,plu+k1,cmp);
for(int i=;i<k1;i++)
{
if(i==) printf("+%s",plu[i].c_str());
else printf(",%s",plu[i].c_str());
}
if(k1>) printf("\n"); sort(dir,dir+k2,cmp);
for(int i=;i<k2;i++)
{
if(i==) printf("-%s",dir[i].c_str());
else printf(",%s",dir[i].c_str());
}
if(k2>) printf("\n"); sort(key,key+k3,cmp);
for(int i=;i<k3;i++)
{
if(i==) printf("*%s",key[i].c_str());
else printf(",%s",key[i].c_str());
}
if(k3>) printf("\n"); }
printf("\n");
}
return ;
}
上一篇:github.com使用方法


下一篇:PayPal 开发详解(七):运行REST API SAMPLE