zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    496 服务器渲染 VS 客户端渲染
    495 队列,优先级队列
    493 JS中数据类型检测的四种方案
    492 js的继承:原型继承,CALL继承,寄生组合式继承,ES6中的类和继承
    491 CALL和APPLY以及BIND语法(含BIND的核心原理),CALL和APPLY的应用(类数组借用数组原型方法),CALL源码解析及阿里面试题
    490 JavaScript的this的五种指向
    488 DOM0和DOM2事件绑定的原理、使用、区别
    487 函数的三种角色:普通函数,构造函数(类),普通对象,原型链清明上河图
    486 原型及原型链模式:3个重要知识点,从面向对象角度来讲解内置类,hasOwnProperty,原型链方法中的THIS问题,基于内置类的原型扩展方法
    485 面向对象:单例设计模式,工厂模式,什么是面向对象,构造函数,instanceof,构造函数中的局部变量以及new构造函数时不加括号
  • 原文地址:https://www.cnblogs.com/jeff-wgc/p/4471533.html
Copyright © 2011-2022 走看看