[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 ( 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
题目大意:每次给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 ...
随机推荐
- 解决selenium不支持firefox低版本的问题
解决selenium不支持firefox低版本的问题 在火狐浏览器升级后,突然发现webdriver运行脚本的时候不能调出火狐浏览器了,并报错WebDriverException:Message:'C ...
- mysql 导出sql结果成csv文件
mysql -uroot -p -e "use database;sql语句:" > a.csv 举例: mysql -uroot -p -e "use main; ...
- VB 共享软件防破解设计技术初探(一)
VB 共享软件防破解设计技术初探(一) ×××××××××××××××××××××××××××××××××××××××××××××× 其他文章快速链接: VB 共享软件防破解设计技术初探(二)http ...
- Win32 Debug & Release
今天帮汤老师调试程序,他生成的程序不能运行,怀疑子程序之间编译顺序的问题:我试了之后,也出现同样的问题,但是把Win32 Debug 换成Win32 Release却可以运行了. 网上搜索了下,在CV ...
- ajax访问当前页面后的 [WebMethod]描述的方法
脚本: function show() { $.ajax({ type: "post", async: false, contentType: "application/ ...
- 【转】MEF程序设计指南五:迟延(Lazy)加载导出部件(Export Part)与元数据(Metadata)
MEF中使用导出与导入,实质上就是对一个对象的实例化的过程,通过MEF的特性降低了对象的直接依赖,从而让系统的设计达到一种高灵活.高扩展性的效果.在具体的设计开发中,存在着某些对象是不需要在系统运行或 ...
- Error writing temporary file. Make sure your temp folder is valid
NSIS Error:Error writing temporary file. Make sure your temp folder is valid的解决 老婆用了自己的WIN7系统一段时 ...
- cocos2d-js 安卓自定义本地通知功能
安卓新手,笔记有理解不当的地方望指出,经过几天折腾终于可以实现类似ios的本地通知功能(ios就几行代码),可能有第三方sdk可以方便实现,暂时没去找 思路: 1. startService 和bin ...
- jquery ajax请求方式与提示用户正在处理请稍等
为了提高用户体验度,我们通常会给出 “正在处理,请稍等!”诸如此类的提示.我们可通过设置$.ajax()下的参数beforeSend()来实现 初次使用$.ajax() ,我没有去区分过ajax的异步 ...
- 2018.06.30 BZOJ4765: 普通计算姬(dfs序+分块+树状数组)
4765: 普通计算姬 Time Limit: 30 Sec Memory Limit: 256 MB Description "奋战三星期,造台计算机".小G响应号召,花了三小时 ...