Uva 12504 - Updating a Dictionary
你为何这么叼!
卧槽,value值居然要string这么大才行,我了个表的!
Updating a Dictionary
Time limit: 1.000 seconds
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:
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
Problemsetter: Rujia Liu, Special Thanks: Feng Chen, Md. Mahbubul Hasan
1 #include <map> 2 #include <string> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 using namespace std; 7 #define maxn 111 8 int n,m; 9 char f[4][maxn][maxn]; 10 map<string,string>a; 11 map<string,string>b; 12 int main(){ 13 int t; 14 scanf("%d",&t); 15 while(t--){ 16 char ch; 17 int k=0; 18 char s1[maxn],s2[maxn],x[maxn]; 19 a.clear();b.clear(); 20 memset(f,0,sizeof f); 21 memset(s1,0,sizeof s1); 22 memset(s2,0,sizeof s2); 23 while(ch!='{')ch=getchar(); 24 while(ch!='}'){ 25 ch=getchar(); 26 if(ch==',')continue; 27 if(ch==':'){ 28 int kk=0; 29 while(ch!=','&&ch!='}'){ 30 ch=getchar(); 31 if(ch==','||ch=='}')break; 32 s2[kk++]=ch; 33 } 34 a[s1]=s2;k=0;memset(s1,0,sizeof s1);memset(s2,0,sizeof s2);} 35 else s1[k++]=ch; 36 } 37 while(ch!='{')ch=getchar(); 38 k=0; 39 while(ch!='}'){ 40 ch=getchar(); 41 if(ch==',')continue; 42 if(ch==':'){ 43 int kk=0; 44 while(ch!=','&&ch!='}'){ 45 ch=getchar(); 46 if(ch==','||ch=='}')break; 47 s2[kk++]=ch; 48 } 49 b[s1]=s2;k=0;memset(s1,0,sizeof s1);memset(s2,0,sizeof s2);} 50 else s1[k++]=ch; 51 } 52 int kt[4]; 53 kt[0]=0,kt[1]=0,kt[2]=0; 54 map<string,string>::iterator it; 55 for(it=b.begin();it!=b.end();it++) 56 if(a.find(it->first)==a.end()){ 57 a[it->first]=it->second; 58 for(int i=0;i<it->first.size();i++)f[0][kt[0]][i]=it->first[i]; 59 kt[0]++; 60 } 61 for(it=a.begin();it!=a.end();) 62 if(b.find(it->first)==b.end()){ 63 for(int i=0;i<it->first.size();i++)f[1][kt[1]][i]=it->first[i]; 64 map<string,string>::iterator jt; 65 jt=it; 66 it++; 67 a.erase(jt); 68 kt[1]++; 69 } 70 else it++; 71 for(it=b.begin();it!=b.end();it++) 72 if(a.find(it->first)->second!=it->second){ 73 for(int i=0;i<it->first.size();i++)f[2][kt[2]][i]=it->first[i]; 74 kt[2]++; 75 } 76 for(int k=0;k<3;k++) 77 for(int i=0;i<kt[k];i++) 78 for(int j=i+1;j<kt[k];j++) 79 if(strcmp(f[k][i],f[k][j])>0){ 80 char temps[maxn]; 81 strcpy(temps,f[k][i]); 82 strcpy(f[k][i],f[k][j]); 83 strcpy(f[k][j],temps); 84 } 85 if(kt[0]==0&&kt[1]==0&&kt[2]==0){printf("No changes\n\n");continue;} 86 for(int k=0;k<3;k++){ 87 if(k==0&&kt[0])printf("+"); 88 if(k==1&&kt[1])printf("-"); 89 if(k==2&&kt[2])printf("*"); 90 for(int i=0;i<kt[k];i++)printf(i==kt[k]-1?"%s\n":"%s,",f[k][i]); 91 } 92 printf("\n"); 93 } 94 return 0; 95 }
浙公网安备 33010602011771号