zoukankan      html  css  js  c++  java
  • H-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,queue,string,stringstream,总之全用的标准库(咳咳咳),打算在重新写一份代码(代码待补),不借助map和queue,还有一件事,这道题提交了好多次都是WA。。。然后就把题读了好几遍,没发现什么特殊的情况存在,然后注释掉了一句代码就过了,std::ios::sync_with_stdio(false),就这句,解除cin与stdin的同步,据说会加快cin的速度,我怕会超时就把这段代码加上了(最后80ms),结果问题就出在这了,还有就是换行cout不用<<endl貌似也能加快速度。。。

    代码:

    #include<iostream>
    #include<sstream>
    #include<string>
    #include<queue>
    #include<map>
    using namespace std;
    
    map<string,string> mp1,mp2;
    typedef queue<string> Queue;
    Queue New,Change,None;
    
    void Replace(string &str){
        int len = str.size();
        for(int i=0;i<len;i++)
            if(str[i] == ':'|| str[i]=='{' || str[i]=='}' || str[i] == ',') str[i] = ' ';
    }
    
    void String_work(string &pre,string now){
        Replace(pre); Replace(now);
        stringstream ss(pre),mm(now);
        string tmp,str;
        while(ss >> str){
            ss >> tmp;
            mp1[str] = tmp;
        }
        while(mm >> str){
            mm >> tmp;
            mp2[str] = tmp;
        }
    }
    
    void Add_to_queue(){
        map<string,string> ::iterator it;
        for(it=mp2.begin();it!=mp2.end();it++){
            if(mp1.find((*it).first)==mp1.end())
                New.push((*it).first);
            else if(mp1[(*it).first]!=(*it).second) {
                Change.push((*it).first);
                mp1.erase((*it).first);
            }
            else mp1.erase((*it).first);
    
        }
        for(it=mp1.begin();it!=mp1.end();it++){
            None.push((*it).first);
        }
    }
    
    void print_ans(Queue &Q,char c){
        cout<< c;
        while(!Q.empty()){
            cout << Q.front(); Q.pop();
            if(Q.empty()) cout << "
    " ;  else cout <<",";
        }
    }
    
    void Solve_question(){
        bool flag = true;
        if(!New.empty()) {flag = false; print_ans(New,'+');}
        if(!None.empty()) {flag = false; print_ans(None,'-');}
        if(!Change.empty()) {flag = false; print_ans(Change,'*');}
        if(flag) cout <<"No changes
    ";
        cout <<"
    ";
    }
    
    void Init_map(){
        mp1.clear(); mp2.clear();
    }
    
    int main(){
        //std::ios::sync_with_stdio(false);!!!不能用!!!切记!!!
        int T;
        string pre,now;
        cin >> T;
        while(T--){
            Init_map();
            cin >> pre >> now;
            String_work(pre,now);
            Add_to_queue();
            Solve_question();
        }
    }


  • 相关阅读:
    解决Enterprise Library January 2006不能加密配置文件的方法
    ASP.NET Ajax 和ASP.NET 2.0 的登陆控件相冲突的问题的讨论
    十二时辰与时间对照表,十二经络时辰表
    对表中数据逐行累加
    SQL脚本 CASE...WHEN...THEN...ELSE...END 的应用
    [转]看刚毕业MM如何在北京买房
    让你的GUI程序随WINDOWS服务一起启动
    启动Oracle,SQL服务,IIS脚本
    无论买新房还是二手房 教你六招可放心收房
    经典开源项目简介及源码下载
  • 原文地址:https://www.cnblogs.com/Pretty9/p/7347669.html
Copyright © 2011-2022 走看看