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 ...
随机推荐
- php 求水仙花数优化
水仙花数是指一个n位数(n>=3),它每一个位上数字的n次幂之和等于它本身,n为它的位数.(比如:1^3+5^3+3^3 = 153) 水仙花数又称阿姆斯特朗数. 三位的水仙花数有4个:153, ...
- mysql 更改自动增长列的初始值
alter table t_Myxiao7 AUTO_INCREMENT 3; -- 从三开始 ITOKIT.COM提示:如果表中数据没有用.如果直接删除数据,自动增长ID还是不会从1开始的,可以 ...
- Cocos2d-x基础篇C++
1.C++类和对象 类的公有成员可以使用成员访问运算符(.)访问. (::)是范围解析运算符.调用成员函数是在对象上使用(.)运算符. 2.C++继承(C++中父类称为基类,子类称为派生类) clas ...
- Linux学习之Shell编程基础
转自:http://my.oschina.net/itblog/blog/204410 1 语法基本介绍1.1 开头 程序必须以下面的行开始(必须方在文件的第一行): #!/bin/sh 符号#!用来 ...
- Hibernate学习之缓存机制
转自:http://www.cnblogs.com/xiaoluo501395377/p/3377604.html 一.N+1问题 首先我们来探讨一下N+1的问题,我们先通过一个例子来看一下,什么是N ...
- hdu 4619 Warm up 2 二分图匹配
题目链接 给两种长方形, 水平的和垂直的, 大小都为1*2, n个水平的, m个垂直的, 给出它们的坐标. 水平的和垂直的可以相互覆盖, 但是同种类型的没有覆盖. 去掉一些长方形, 使得剩下的全部都没 ...
- IIs 网站应用程序与虚拟目录的区别及高级应用说明(文件分布式存储方案)
原文 IIs 网站应用程序与虚拟目录的区别及高级应用说明(文件分布式存储方案) 对于IIS网站,大伙用的比较多,就不啰嗦了. 今天和说说大伙比较少使用的"IIS应用程序”和虚拟目录的区别 ...
- 启用Apache Mod_rewrite模块
Ubuntu 环境 在终端中执行 sudo a2enmod rewrite 指令后,即启用了 Mod_rewrite 模块. 另外,也可以通过将 /etc/apache2/mods-available ...
- WIZnet即将推出高性能网络芯片W5500
WIZnet将于9月份推出高性能网络芯片W5500,这是继W5100.W5200和W5300之后一款全新的全硬件TCP/IP协议栈网络芯片,这款芯片具有更低功耗与工作温度,及改良工艺,是嵌入式以太网的 ...
- C++模板:qsort
void qsort(int l,int r){ int i,j,t,mid; mid=b[(l+r)>>1]; i=l; j=r; do{ while (b[i]<mid) i++ ...