[ACM_模拟] UVA 12504 Updating a Dictionary [字符串处理 字典增加、减少、改变问题]
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:
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 ( T1000). 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
题目大意:每次给2个字符串,字符串里的内容表示为key:value对,顺序随意,比较2个字符串里的内容判断增加了那些,减少了那些,key值对应的value变了的有哪些。水题,字符串处理,烦!
#include<iostream>
#include<cstdio>
#include<string>
#include<string.h>
#include<cstring>
#include<sstream>
#include<algorithm>
using namespace std;
struct A
{
string key;
string value;
int same(A &b){
if(key==b.key){
if(value==b.value)return ;//no change
else return ;//change
}
else{//not same
if(key<b.key)return ;
else return ;
}
}
void set(string a,string b){
key=a;
value=b;
}
};
bool cmp(A a,A b){
return a.key<b.key;
}//比较函数一定不要用&同名引用
void xiu(string &A){
A=A.substr(,A.length()-);
for(int i=A.length()-;i>=;i--){
if(A[i]==',' || A[i]==':')A[i]=' ';
}
}
int main(){
int T;cin>>T;
string str1;
string str2;
string value,key;
getline(cin,str1);
while(T--){
getline(cin,str1);
getline(cin,str2);
xiu(str1);
xiu(str2);
istringstream in1(str1);
istringstream in2(str2);
A x1[],x2[];
int i=;
while(in1>>key>>value){
x1[i++].set(key,value);
}
int j=;
while(in2>>key>>value){
x2[j++].set(key,value);
}
sort(x1,x1+i,cmp);
sort(x2,x2+j,cmp);
string add[];int add_num=;
string sub[];int sub_num=;
string cha[];int cha_num=;
int ii=,jj=;
while(ii<i && jj<j){
switch(x1[ii].same(x2[jj])){
case :ii++,jj++;break;
case :cha[cha_num++]=x1[ii].key;ii++,jj++;break;
case :sub[sub_num++]=x1[ii].key;ii++;break;
case :add[add_num++]=x2[jj].key;jj++;break;
default:break;
}
}
while(ii<i){
sub[sub_num++]=x1[ii++].key;
}
while(jj<j){
add[add_num++]=x2[jj++].key;
}
if(add_num+sub_num+cha_num==)cout<<"No changes\n\n";
else{
if(add_num!=){
cout<<"+"<<add[];
for(int k=;k<add_num;k++){
cout<<','<<add[k];
}
cout<<'\n';
}
if(sub_num!=){
cout<<"-"<<sub[];
for(int k=;k<sub_num;k++){
cout<<','<<sub[k];
}
cout<<'\n';
}
if(cha_num!=){
cout<<'*'<<cha[];
for(int k=;k<cha_num;k++){
cout<<','<<cha[k];
}
cout<<'\n';
}
cout<<'\n';
}
}return ;
}
[ACM_模拟] UVA 12504 Updating a Dictionary [字符串处理 字典增加、减少、改变问题]的更多相关文章
- Uva - 12504 - Updating a Dictionary
全是字符串相关处理,截取长度等相关操作的练习 AC代码: #include <iostream> #include <cstdio> #include <cstdlib& ...
- Uva 511 Updating a Dictionary
大致题意:用{ key:value, key:value, key:value }的形式表示一个字典key表示建,在一个字典内没有重复,value则可能重复 题目输入两个字典,如{a:3,b:4,c: ...
- 【UVA】12504 Updating a Dictionary(STL)
题目 题目 分析 第一次用stringstream,真TMD的好用 代码 #include <bits/stdc++.h> using namespace std; int ...
- [ACM_模拟] UVA 10881 Piotr's Ants[蚂蚁移动 数组映射 排序技巧]
"One thing is for certain: there is no stopping them;the ants will soon be here. And I, for one ...
- [ACM_模拟] UVA 12503 Robot Instructions [指令控制坐标轴上机器人移动 水]
Robot Instructions You have a robot standing on the origin of x axis. The robot will be given som ...
- 湖南生第八届大学生程序设计大赛原题 C-Updating a Dictionary(UVA12504 - Updating a Dictionary)
UVA12504 - Updating a Dictionary 给出两个字符串,以相同的格式表示原字典和更新后的字典.要求找出新字典和旧字典的不同,以规定的格式输出. 算法操作: (1)处理旧字典, ...
- csuoj 1113: Updating a Dictionary
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1113 1113: Updating a Dictionary Time Limit: 1 Sec ...
- [刷题]算法竞赛入门经典(第2版) 5-11/UVa12504 - Updating a Dictionary
题意:对比新老字典的区别:内容多了.少了还是修改了. 代码:(Accepted,0.000s) //UVa12504 - Updating a Dictionary //#define _XieNao ...
- [Swift]JSON字符串与字典(Dictionary)、数组(Array)之间的相互转换
1.JSON字符串与字典(Dictionary)之间的相互转换 import Foundation //JSON字符串转换为字典(Dictionary) func getDictionaryFromJ ...
随机推荐
- http头解释
<---响应头---> 长连接: Connection: keep-alive 开启长连接 ---- connection 英 [kəˈnekʃn]连接 ...
- Jmeter分布式
Jmeter运行时十分耗CPU和内存,在实际应用中有时一台机器不能满足测试要求,这时就需要利用多台机器来进行分布式. Jmeter分布式的测试框架:框架中所有的测试脚本都要从测试主机传送到测试从机 ...
- 结对编程--fault,error,failure
结对编程对象:叶小娟 对方博客地址:http://www.cnblogs.com/yxj63/ 双方贡献比例:1:1 结对照片: 结对题目:输入一定个数的数字,对其排序后输出最大值. 1 pack ...
- python之面向对象之继承
#写一个类SchoolMember class SchoolMember(object): member_num = 0 def __init__(self,name,age,sex): self.n ...
- mysqlbateis generator 当遇到tinyint 生成转化bool 解决方法
当遇到tyint 生成转化bool 类型问题很恶心,记录一下解决方法 一. TinyInt转换规则 JAVA数据类型 和 MYSQL的数据类型转换,要注意tinyInt 类型,且存储长度为1的情况. ...
- 10-多写一个@Autowired导致程序崩了
再是javaweb实验六中,是让我们改代码,让它跑起来,结果我少注释了一个,导致一直报错,检查许久没有找到,最后通过代码替换逐步查找,才发现问题.
- struts框架值栈问题三之值栈的创建和ActionContext对象的关系
3. 问题三 : 值栈对象的创建,ValueStack 和 ActionContext 是什么关系? * 值栈对象是请求时创建的 * ActionContext是绑定到当前的线程上(一个Action访 ...
- Ubuntu下安装VirtualBox并为其添加USB支持
1.下载VirtualBox软件包和USB支持包 下载网址均为官方网站(可在此查看其使用教程):https://www.virtualbox.org/wiki/Downloads (若下载各平台各版本 ...
- code1002 搭桥
最小生成树 每读入一个城市,把他与之前的所有城市做一次link() link的内容: 1.如果两个城市直接相连,合并他们的集合(并查集)2.如果两个城市可以搭桥,添加一条边来连接.如果不可以搭桥,什么 ...
- Vue2.0 keep-alive 组件的最佳实践
1.基本用法 vue2.0提供了一个keep-alive组件用来缓存组件,避免多次加载相应的组件,减少性能消耗 <keep-alive> <component> <!-- ...