zoukankan      html  css  js  c++  java
  • CSU 1113 Updating a Dictionary

    传送门

    Time Limit: 1000MS   Memory Limit: 131072KB   64bit IO Format: %lld & %llu

    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 

    ------------------------------------------------------------------------------

     简单题,注意implementation 
    ------------------------------------------------------------------------------
     WA
    #include <cstdio>
    #include <iostream>
    #include <cctype>
    #include <map>
    #include <vector>
    #include <string>
    #include <algorithm>
    #define pb push_back
    
    using namespace std;
    map<string, string> a, b;
    vector<string> aa, bb;
    int main(){
    	//freopen("in", "r", stdin);
    	int T;
    	string s, t;
    	char ch;
    	for(cin>>T; T--; cout<<endl){
    		for(;cin>>ch;){
    			if(isalpha(ch)) s+=ch;
    			else if(ch==':'){
    				while(cin>>ch, isdigit(ch)){
    					t+=ch;
    				}
    				a[s]=t;
    				aa.pb(s);
    				s.clear();
    				t.clear();
    				if(ch=='}') break;	//error
    			}
    		}
    		for(;cin>>ch;){
    			if(isalpha(ch)) s+=ch;
    			else if(ch==':'){
    				while(cin>>ch, isdigit(ch)){
    					t+=ch;
    				}
    				bb.pb(s);
    				b[s]=t;
    				s.clear();
    				t.clear();
    				if(ch=='}') break;	//error
    			}
    		}
    		sort(aa.begin(), aa.end());
    		sort(bb.begin(), bb.end());
    		bool fir=true, nc=true;
    		for(int i=0; i<bb.size(); i++){
    			string &tmp=bb[i];
    			if(a[tmp].empty()){
    				if(fir) cout<<'+'+tmp, fir=false;
    				else cout<<','+tmp;
    			}
    		}
    		if(!fir) cout<<endl, nc=0;
    		fir=true;
    		for(int i=0; i<aa.size(); i++){
    			string &tmp=aa[i];
    			if(b[tmp].empty()){
    				if(fir) cout<<'-'+tmp, fir=false;
    				else cout<<','+tmp;
    			}
    		}
    		if(!fir) cout<<endl, nc=0;
    		fir=true;
    		for(int i=0; i<bb.size(); i++){
    			string &tmp=bb[i];
    			if(!a[tmp].empty()&&!b[tmp].empty()&&a[tmp]!=b[tmp]){
    				if(fir) cout<<'*'+tmp, fir=false;
    				else cout<<','+tmp;
    			}
    		}
    		if(!fir) cout<<endl, nc=0;
    		if(nc) cout<<"No changes"<<endl;
    		a.clear(), b.clear(), aa.clear(), bb.clear();
    	}
    
    }
    
     AC 
    #include <cstdio>
    #include <iostream>
    #include <cctype>
    #include <map>
    #include <vector>
    #include <string>
    #include <algorithm>
    #define pb push_back
    
    using namespace std;
    map<string, string> a, b;
    vector<string> aa, bb;
    int main(){
    	freopen("in", "r", stdin);
    	int T;
    	string s, t;
    	char ch;
    	for(cin>>T; T--; cout<<endl){
    		for(;cin>>ch;){
    			if(isalpha(ch)) s+=ch;
    			else if(ch==':'){
    				while(cin>>ch, isdigit(ch)){
    					t+=ch;
    				}
    				a[s]=t;
    				aa.pb(s);
    				s.clear();
    				t.clear();
    			}
    			if(ch=='}') break;
    		}
    		//cout<<aa.size()<<endl;
    		for(;cin>>ch;){
    			if(isalpha(ch)) s+=ch;
    			else if(ch==':'){
    				while(cin>>ch, isdigit(ch)){
    					t+=ch;
    				}
    				bb.pb(s);
    				b[s]=t;
    				s.clear();
    				t.clear();
    			}
    			if(ch=='}') break;
    		}
    		sort(aa.begin(), aa.end());
    		sort(bb.begin(), bb.end());
    		bool fir=true, nc=true;
    		for(int i=0; i<bb.size(); i++){
    			string &tmp=bb[i];
    			if(a[tmp].empty()){
    				if(fir) cout<<'+'+tmp, fir=false;
    				else cout<<','+tmp;
    			}
    		}
    		if(!fir) cout<<endl, nc=0;
    		fir=true;
    		for(int i=0; i<aa.size(); i++){
    			string &tmp=aa[i];
    			if(b[tmp].empty()){
    				if(fir) cout<<'-'+tmp, fir=false;
    				else cout<<','+tmp;
    			}
    		}
    		if(!fir) cout<<endl, nc=0;
    		fir=true;
    		for(int i=0; i<bb.size(); i++){
    			string &tmp=bb[i];
    			if(!a[tmp].empty()&&!b[tmp].empty()&&a[tmp]!=b[tmp]){
    				if(fir) cout<<'*'+tmp, fir=false;
    				else cout<<','+tmp;
    			}
    		}
    		if(!fir) cout<<endl, nc=0;
    		if(nc) cout<<"No changes"<<endl;
    		a.clear(), b.clear(), aa.clear(), bb.clear();
    	}
    }
    
  • 相关阅读:
    jdk1.8 LongAdder源码学习
    linux 下 vi 文本编辑如何复制一行粘贴删除一行数据
    远程调试
    本机与远程主机指定端口的网络是否连通
    自定义弹窗
    Windows查看占用端口的进程及其对应的应用程序并关闭之
    超实惠:99元阿里云服务器1核2G内存40G硬盘(SSD)
    Java显式锁学习总结之六:Condition源码分析
    Maven使用国内镜像
    深入理解读写锁—ReadWriteLock源码分析
  • 原文地址:https://www.cnblogs.com/Patt/p/4730103.html
Copyright © 2011-2022 走看看