[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 ...
随机推荐
- HTTP协议图示详解
一.概念 协议是指计算机通信网络中两台计算机之间进行通信所必须共同遵守的规定或规则,超文本传输协议(HTTP)是一种通信协议,它允许将超文本标记语言(HTML)文档从Web服务器传送到客户端的浏览器. ...
- 安装使用phpStudy在本机配置php运行环境
前言: php开发的初学者,强烈推荐使用phpStudy集成环境,一方面这个的确很好用(本人电脑安装了jspStudy,可以同时调试php和jsp),另一方面呢,虽然本人是技术控,但对这些繁杂的安装部 ...
- TZOJ 2755 国际象棋(广搜+哈希)
描述 在n*n的国际象棋棋盘中,给定一“马(Knight)”和一“后(Queen)”的位置,问“马”能否在m步之内(包括m步)到达“后”的位置?马的走法是:每步棋先横走或直走一格,然后再斜走一格,即走 ...
- python之生产者消费者模型
#Auther Bob #--*--conding:utf-8 --*-- #生产者消费者模型,这里的例子是这样的,有一个厨师在做包子,有一个顾客在吃包子,有一个服务员在储存包子,这个服务员我们就可以 ...
- OC 线程操作 - GCD使用 - 栅栏函数
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //同步函数无需栅栏函数 //栅栏 ...
- php 中的信号处理
首先我们需要了解几个函数 pcntl_signal 安装信号处理器,也就是当指定信号发生时,调用函数. pcntl_alarm 指定秒数后向进程发送SIGALRM信号. posix_getpi ...
- mysql5.5以上开启慢查询
在my.ini配置文件中添加: [mysqld] #开启慢查询 slow_query_log = on #慢查询时间 long_query_time = 0.5 #记录没有使用索引的查询 log_qu ...
- iOS开发总结
最近在工作中使用Objective-C开发iOS客户端程序,它一方面和Server通讯,处理网络连接,收发报文,实现业务逻辑;另一方面为UI层提供各种业务API. 下面记录用到的相关知识点,困难, ...
- hive的用户和用户权限
HiverServer2支持远程多客户端的并发和认证,支持通过JDBC.Beeline等连接操作.hive默认的Derby数据库,由于是内嵌的文件数据库,只支持一个用户的操作访问,支持多用户需用mys ...
- [精彩] 关于DB2的内存分配
这两天在看DB2的内存管理的内容,看的很是模糊,有以下问题不明白,请教 是不是数据库管理器的共享内存就是DB2能够使用的最大内容呢,然后数据库全局内存从管理器内存那里获得分配的内存,然后应用程序全局内 ...