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代码:

  1 #include<cstring>
  2 #include<cstdio>
  3 #include<algorithm>
  4 #include<iostream>
  5 #include<string>
  6 #include <cctype>
  7 #include<map>
  8 using namespace std;
  9 char s1[1200];
 10 char s2[1200];
 11 char s3[1200];
 12 char s4[1200];
 13 bool cmp(string ss,string sss)
 14 {
 15     return ss<sss;
 16 }
 17 string st,ed;
 18 int main()
 19 {
 20     int T,t1,t2;
 21     scanf("%d",&T);
 22     gets(s1);
 23     while(T--)
 24     {   map<string,string>ss1;
 25         map<string,string>ss2;
 26         map<string,string>::iterator it;
 27         string ss[100];
 28         string plu[1000],dir[1000],key[1000];
 29         int k1 =0 ,k2 =0,k3 =0,k4=0;
 30         ss1.clear();
 31         ss2.clear();
 32         for(int i=0;i<100;i++)
 33         {
 34              ss[i].clear();
 35              plu[i].clear();
 36              dir[i].clear();
 37              key[i].clear();
 38         }
 39         gets(s1);
 40         gets(s2);
 41          st = ed = "";
 42         int len1 = strlen(s1);
 43         int len2 = strlen(s2);
 44         for(int i=1;i<len1;i++)
 45         {
 46             if(isdigit(s1[i]))
 47                 ed = ed+s1[i];
 48             else if(isalpha(s1[i]))
 49                  st = st+s1[i];
 50             else if(s1[i] == ',')
 51             {
 52                 ss1[st] =ed;
 53                 ss[k4++] = st;
 54                 st = ed = "";
 55             }
 56             else if(s1[i] == '}')
 57             {
 58                 if(st!="")
 59                 {
 60                     ss[k4++] = st;
 61                     ss1[st] =ed;
 62                 }
 63                  st = ed = "";
 64             }
 65         }
 66         for(int i=1;i<len2;i++)
 67         {
 68             if(isdigit(s2[i]))
 69                 ed =ed+s2[i];
 70             else if(isalpha(s2[i]))
 71                  st = st+s2[i];
 72             else if(s2[i] == ',')
 73             {
 74                  it = ss1.find(st);
 75                     if(it!=ss1.end())//如果找到
 76                     {
 77                         if(ss1[st] != ed)//增加的
 78                             key[k3++] = st;
 79                     }
 80                     else
 81                     {
 82                         plu[k1++] = st;
 83                     }
 84                 ss2[st] =ed;
 85                 st = ed = "";
 86             }
 87             else if(s2[i] == '}')
 88             {
 89                 if(st!="")
 90                 {
 91                     it = ss1.find(st);
 92                     if(it!=ss1.end())//如果找到
 93                         {
 94                             if(ss1[st] != ed)//增加的
 95                                key[k3++] = st;
 96                         }
 97                         else
 98                         {
 99                             plu[k1++] = st;
100                         }
101                   ss2[st] =ed;
102                 }
103                  st = ed = "";
104             }
105         }
106         for(int i=0;i<k4;i++)
107         {
108             it = ss2.find(ss[i]);
109             if(it == ss2.end())//如果没有找到
110                 {
111                     dir[k2++] = ss[i];
112                 }
113         }
114         if(k1+k2+k3 == 0)
115             printf("No changes
");
116         else
117         {
118             sort(plu,plu+k1,cmp);
119             for(int i=0;i<k1;i++)
120             {
121                 if(i==0) printf("+%s",plu[i].c_str());
122                 else printf(",%s",plu[i].c_str());
123             }
124             if(k1>0) printf("
");
125 
126             sort(dir,dir+k2,cmp);
127             for(int i=0;i<k2;i++)
128             {
129                 if(i==0) printf("-%s",dir[i].c_str());
130                 else printf(",%s",dir[i].c_str());
131             }
132             if(k2>0) printf("
");
133 
134             sort(key,key+k3,cmp);
135             for(int i=0;i<k3;i++)
136             {
137                 if(i==0) printf("*%s",key[i].c_str());
138                 else printf(",%s",key[i].c_str());
139             }
140             if(k3>0) printf("
");
141 
142         }
143         printf("
");
144     }
145     return 0;
146 }
View Code
原文地址:https://www.cnblogs.com/jeff-wgc/p/4471533.html