[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 ...
随机推荐
- CSS的浮动(float)
问题:在练习过程中,发现div1浮动后,它下面的div被覆盖住了. 解决方案:清除该div1的浮动. 关于CSS的浮动 1.div是块级元素,独占一行 2.浮动可以理解为让某个div元素脱离标准流,漂 ...
- Ajax 与 jquery
jquery 里面的ajax用法: $.ajax({ 参数设置: 如果返回数据不是json的时候,记得转化为json . var data = json.parse(data); json 可以直接点 ...
- 第七章 二叉搜索树 (d2)AVL树:插入
- ubuntu 下当前网速查看
ubuntu下用ethstatus可以监控实时的网卡带宽占用.这个软件能显示当前网卡的 RX 和 TX 速率,单位是Byte 一.安装 ethstatus 软件 #sudo apt-get insta ...
- mysql增加远程连接用户及查看数据库表结构
一.增加远程连接用户 1.用root权限登录数据库 2.加用户:grant all privileges on *.* to '111'@'192.168.1.%' identified by '2 ...
- VirtualBox中的虚拟机在Ubuntu 下无法启动之问题解决
我通过重新安装box解决的.发现,box安装,直接apt install有可能会出现虚拟机无法启动的问题. 一.添加VirtualBox的源并安装5.1版本virtualbox官网:https://w ...
- [leetcode]215. Kth Largest Element in an Array 数组中第k大的元素
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- vmware14中安装centos7并使用docker发布spring-boot项目
1.vmare中centos7安装(同一路由器无线网络下) 1.1选择桥接模式 1.2修改配置文件 vi /etc/sysconfig/network-scripts/ifcfg-ens33(这里不一 ...
- 源码安装php时出现Sorry, I cannot run apxs. Possible reasons follow:
1.可能的原因是你没有安装perl > yum install perl > yum install httpd-devel 2.在你apache安装目录下的bin下找到apxs,并用vi ...
- iOS.NSString.pitfall-in-using-nsstring
1. NSString的使用 在CodeReview中, 发现类似以下代码, 表示深深受伤了: NSString* fString = [NSString stringWithFormat:@&quo ...