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;
}
Uva 511 Updating a Dictionary的更多相关文章
- [ACM_模拟] UVA 12504 Updating a Dictionary [字符串处理 字典增加、减少、改变问题]
Updating a Dictionary In this problem, a dictionary is collection of key-value pairs, where keys ...
- Uva - 12504 - Updating a Dictionary
全是字符串相关处理,截取长度等相关操作的练习 AC代码: #include <iostream> #include <cstdio> #include <cstdlib& ...
- [刷题]算法竞赛入门经典(第2版) 5-11/UVa12504 - Updating a Dictionary
题意:对比新老字典的区别:内容多了.少了还是修改了. 代码:(Accepted,0.000s) //UVa12504 - Updating a Dictionary //#define _XieNao ...
- csuoj 1113: Updating a Dictionary
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1113 1113: Updating a Dictionary Time Limit: 1 Sec ...
- 湖南生第八届大学生程序设计大赛原题 C-Updating a Dictionary(UVA12504 - Updating a Dictionary)
UVA12504 - Updating a Dictionary 给出两个字符串,以相同的格式表示原字典和更新后的字典.要求找出新字典和旧字典的不同,以规定的格式输出. 算法操作: (1)处理旧字典, ...
- Problem C Updating a Dictionary
Problem C Updating a Dictionary In this problem, a dictionary is collection of key-value pairs, ...
- 【习题 5-11 UVA 12504 】Updating a Dictionary
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 不确定某个map里面是否有某个关键字的时候. 要用find来确定. 如果直接用访问下标的形式去做的话. 会强行给他加一个那个关键字( ...
- Updating a Dictionary UVA - 12504
In this problem, a dictionary is collection of key-value pairs, where keys are lower-case letters, a ...
- 【UVA】12504 Updating a Dictionary(STL)
题目 题目 分析 第一次用stringstream,真TMD的好用 代码 #include <bits/stdc++.h> using namespace std; int ...
随机推荐
- IOS学习之蓝牙4.0 BLE
IOS学习也一段时间了,该上点干货了.前段时间研究了一下IOS蓝牙通讯相关的东西,把研究的一个成果给大家分享一下. 一 项目背景 简单介绍一下做的东西,设备是一个金融刷卡器,通过蓝牙与iphone手机 ...
- 怎样用Excel自动排成绩
经常需要做表格,排成绩名次总是笨笨的一个一个填,费时又费力,终于找到了解决的办法%>_<%(不要嘲笑我了(✿◡‿◡)),原来就是一个名叫RANK的函数,还在苦逼地自己输的小伙伴们,快来看吧 ...
- C#中小函数的应用
今天看到的一段函数 StringBuilder sb = new StringBuilder("sselect * from table where 1=1"); if(TextB ...
- ASP.NET 使用My97DatePicker日期控件
首先要下载该控件的包,下载地址:http://pan.baidu.com/s/1Aa5gk 引用文件 <script src="js/My97DatePicker/WdatePicke ...
- Linux远程连接与常用命令
要学linux ,一定得用命令界面的,怎么也得是shell语言,用就最难最原始的,用的人都是专家,历史最少也得30年,不管有三七二十一上来就敲ls ,先看看当前目录都有什么.一口专业的linux范儿, ...
- linux如何关闭selinux?
首先我们可以用命令来查看selinux的状态getenforce 这个命令可以查看到selinux的状态,当前可以看到是关闭状态的.还有一个命令也可以查看出selinux的状态.sestatus -v ...
- C# 方法的可选参数、命名参数
原文 http://www.cnblogs.com/lonelyxmas/admin/EditPosts.aspx?opt=1 C#方法的可选参数是.net 4.0最新提出的新的功能,对应简单的重载可 ...
- ccMacros
#ifndef __CCMACROS_H__ #define __CCMACROS_H__ #ifndef _USE_MATH_DEFINES #define _USE_MATH_DEFINES #e ...
- RGB与HSB之间的转换公式
先来了解一些概念: 1.RGB是一种加色模型,就是将不同比例的Red/Green/Blue混合在一起得到新颜色.通常RGB颜色模型表示为: 2.HSB(HSV) 通过色相/饱和度/亮度三要素来表达颜色 ...
- 阵列中条带(stripe)、stripe unit
摘抄:http://blog.sina.com.cn/s/blog_4a362d610100aed2.html 在磁盘阵列中,数据是以条带(stripe)的方式贯穿在磁盘阵列所有硬盘中的.这种数据的分 ...