Uva 511 Updating a Dictionary

大致题意:用{ key:value, key:value, key:value }的形式表示一个字典key表示建,在一个字典内没有重复,value则可能重复    题目输入两个字典,如{a:3,b:4,c:10,f:16}  {a:3,c:5,d:10,ee:4}

    于新字典相比,找出旧字典的不同,并且输出用相应格式,增加的用‘+‘,如 +d,ee

                              减少的用‘-‘,如 -b,f

                              value变化的用*,如 *c

    没有不同则输出"No changes""

如果使用java,则可以用TreeMap对新旧字典做映射,然后对两个TreeMap对象做比较,同时注意一些小细节:

  

import java.util.*;

public class Main{
	public static void main( String []args ) {

		Scanner in = new Scanner( System.in );
		int T = Integer.parseInt( in.nextLine() );
		while( T!=0 ) {
			T --;
			String oldStr = in.nextLine();
			TreeMap<String, String> oldMap = new TreeMap<>();
			str2Map(oldStr, oldMap);
			String newStr = in.nextLine();
			TreeMap<String, String> newMap = new TreeMap<>();
			str2Map(newStr, newMap);
			// 三个函数都要执行
			boolean isChanged = addOrDel(newMap, oldMap, ‘+‘); // 新字典中增加
			isChanged = addOrDel(oldMap, newMap, ‘-‘) || isChanged; // 旧字典中减少
			isChanged = isUpdate( newMap, oldMap, ‘*‘ ) || isChanged; // 更改过的
			if( !isChanged ) {
				System.out.println("No changes");
			}
			System.out.println();
		}
	}

	public static void str2Map( String str, TreeMap<String, String> map ) {
		StringBuffer strbuffer = new StringBuffer(str);
		for( int i=0; i<strbuffer.length(); i ++ ) {
			if( !Character.isLetterOrDigit( strbuffer.charAt(i) ) ) {
				// 将‘{‘,‘}‘,‘:‘,‘,‘修改为空格
				strbuffer.setCharAt(i, ‘ ‘);
			}
		}
		// 以空格为分割符进行分割
		StringTokenizer st = new StringTokenizer( new String(strbuffer), " ");
		while( st.hasMoreTokens() ) {
			map.put( st.nextToken() , st.nextToken() );
		}
	}

	public static boolean addOrDel( TreeMap<String, String> oldMap, TreeMap<String, String> newMap, char op ) {
		boolean isChanged = false;
		for( java.util.Map.Entry<String, String> entry : oldMap.entrySet()){
	           String strkey = entry.getKey();
	           // 只有其中一个字典有,而另一个字典没有
	           if( !newMap.containsKey(strkey) ) {
	        	   if( !isChanged ) {
	        		   System.out.print( op+strkey );
	        		   isChanged = true;
	        	   } else {
	        		   System.out.print( ","+strkey );
	        	   }
	           }
	       }
		if( isChanged ) {
			System.out.println();
		}
		return isChanged;
	}

	public static boolean isUpdate( TreeMap<String, String> oldMap, TreeMap<String, String> newMap, char op ) {
		boolean isChanged = false;
		for( java.util.Map.Entry<String, String> entry : oldMap.entrySet() ) {
			String strkey = entry.getKey();
			// 新旧字典都有,但value不等
			if( newMap.containsKey(strkey) && !newMap.get(strkey).equals( oldMap.get(strkey) ) ) {
				if( !isChanged ) {
					System.out.print( op + strkey );
					isChanged = true;
				} else {
					System.out.print( "," + strkey );
				}
			}
		}
		if( isChanged ) {
			System.out.println();
		}
		return isChanged;
	}
}

如果是C++,也是类似的,用map做映射,也是注意一些细节即可

/*
    UvaOJ 12504
    Emerald
    Sat 27 Jun 2015
*/
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cctype>
#include <map>
#include <sstream>
#include <string>

using namespace std;

string Standard( string str ) {
    for( int i=0; i<str.length(); i++ ) {
        if( !isalpha( str[i] ) && !isdigit( str[i] ) ) {
            str[i] = ‘ ‘;
        }
    }
    return str;
}

void WriteMap( string& dict, std::map<string, string>& m ) {
    dict = Standard( dict );
    stringstream ss( dict );
    string tmp, tmp2;
    while( ss >> tmp ) {
        ss >> tmp2;
        m[ tmp ] = tmp2;
    }
}

bool CompareDict( std::map<string, string>& oldMap, std::map<string, string>& newMap, char op ) {
    std::map<string, string>::iterator it;
    bool isChanged = false;
    for( it=newMap.begin(); it!=newMap.end(); it ++ ) {
        if( !oldMap.count( it->first ) ) {
            if( !isChanged ) {
                printf("%c%s", op, it->first.c_str() );
                isChanged = true;
            } else {
                printf(",%s", it->first.c_str());
            }
        }
    }
    if( isChanged ) {
        printf("\n");
    }
    return isChanged;
}

bool Update( std::map<string, string>& oldMap, std::map<string, string>& newMap ) {
    bool isChanged = false;
    std::map<string, string>::iterator it;
    for( it=oldMap.begin(); it!=oldMap.end(); it ++ ) {
        if( newMap.count( it->first ) && newMap[it->first]!=oldMap[it->first] ) { // defferent items
            if( !isChanged ) {
                printf("*%s", it->first.c_str() );
                isChanged = true;
            } else {
                printf(",%s", it->first.c_str());
            }
        }
    }
    if( isChanged ) {
        printf("\n");
    }
    return isChanged;
}

int main() {
    int T;
    cin >> T;
    cin.get();
    while( T -- ) {
        std::map<string, string> oldMap, newMap;
        string oldDict;
        getline( cin, oldDict );
        WriteMap( oldDict, oldMap );
        string newDict;
        getline( cin, newDict );
        WriteMap( newDict, newMap );
        bool isChanged = CompareDict( oldMap, newMap, ‘+‘ ); // new add
        isChanged = CompareDict( newMap, oldMap, ‘-‘ ) || isChanged ; // old del
        isChanged = Update( oldMap, newMap ) || isChanged;
        if( !isChanged ) {
            printf("No changes\n");
        }
        printf("\n"); // blank line
    }
    return 0;
}
时间: 2024-11-06 07:34:28

Uva 511 Updating a Dictionary的相关文章

UVa 12504 Updating a Dictionary(更新字典)

题意  比较两个字典  按字典序输出所有添加 删除 修改的项   如果没有任何更新  输出  No changes STL map的应用  对比两个字典  注意开始字符串的处理和字典可以为空 #include<bits/stdc++.h> using namespace std; map<string, string> d[2]; map<string, string>::iterator it; const int N = 105; string s, a, b, t

UVA 12504 Updating a Dictionary

题目链接:https://vjudge.net/problem/UVA-12504 题目翻译摘自<算法禁赛入门经典> 题目大意 在本题中,字典是若干键值对,其中键为小写字母组成的字符串,值为没有前导零或正号的非负整数(-4,03 和 +77 都是非法的,注意该整数可以很大).输入一个旧字典和一个新字典,计算二者的变化.输入的两个字典中键都是唯一的,但是排列顺序任意. 输入包含两行,各包含不超过100个字符,即旧字典和新字典.输出格式如下: 如果至少有一个新增键,打印一个“+”号,然后是所有新增

【UVA】12504 - Updating a Dictionary(map,string,vector模拟)

一般的模拟题,一开始WA,可能是用string的容器放char,改成string就过了 14073581 12504 Updating a Dictionary Accepted C++ 0.032 2014-08-21 07:12:19 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<vector> #include<st

Uva 10815-Andy&#39;s First Dictionary(串)

Problem B: Andy's First Dictionary Time limit: 3 seconds Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him, as the number of words that he knows is, well, not quite enough. Instead of thinking up all

UVA 10815-Andy&#39;s First Dictionary(字符串模拟+排序+重复删除)

Andy's First Dictionary Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description Problem B: Andy's First Dictionary Time limit: 3 seconds Andy, 8, has a dream - he wants to produce his very own dictionary. This

【UVA】12504 Updating a Dictionary(STL)

题目 题目 ? ? 分析 第一次用stringstream,真TMD的好用 ? ? 代码 #include <bits/stdc++.h> using namespace std; int main() { int n; cin>>n; getchar();//回车 while(n--) { string s1,s2; getline(cin,s1); getline(cin,s2); for(int i=0;i<s1.length();i++) if(!isalpha(s1

CSU 1113 Updating a Dictionary(map容器应用)

题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1113 解题报告:输入两个字符串,第一个是原来的字典,第二个是新字典,字典中的元素的格式为一个关键字对应一个值,输入格式如下: {a:3,b:4,c:10,f:6} {a:3,c:5,d:10,ee:4}冒号前面的表示关键字,冒号后面的数字表示值,关键字由小写字母组成.现在让你判断,如果新的字典相对于原来的字典有新增的关键字以以下格式输出 :+key1,key2,....如果新的字典相对

更新字典 (Updating a Dictionary,UVa12504)

题目描述: 解题思路: 1.根据:和,获得字符串 2.使用两个map进行比较: 1 #include <iostream> 2 #include <algorithm> 3 #include <string> 4 #include <map> 5 using namespace std; 6 map<string, string>::iterator it; 7 map<string,string> dict[2]; 8 string

Uva 10815-Andy&amp;#39;s First Dictionary(串)

Problem B: Andy's First Dictionary Time limit: 3 seconds Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him, as the number of words that he knows is, well, not quite enough. Instead of thinking up all