Uva - 12504 - Updating a Dictionary
全是字符串相关处理,截取长度等相关操作的练习
AC代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
int T;
cin >> T;
while (T--) {
map<string, string> ma;
string dictOld, dictNew;
string ad[102], del[102], cha[102];
// 分别存储增加的,减少的,变化的关键字
cin >> dictOld >> dictNew;
int addCount = 0;
int deleteCount = 0;
int changeCount = 0;
int a = 0;
while (dictOld.find(',', a + 1) < dictOld.size()) {
// 这里截取方法比较重要
string s(dictOld, a + 1, dictOld.find(',', a + 1) - a - 1);
string kay(s, 0, s.find(':', 0));
string value(s, s.find(':', 0) + 1, s.size() - s.find(':', 0));
ma.insert(pair<string, string>(kay, value));
a = dictOld.find(',', a + 1);
}
string s(dictOld, a + 1, dictOld.find('}', a + 1) - a - 1);
string kay(s, 0, s.find(':', 0));
string value(s, s.find(':', 0) + 1, s.size() - s.find(':', 0));
if (kay.size()) {
ma.insert(pair<string, string>(kay, value));
}
a = 0;
while (dictNew.find(',', a + 1) < dictNew.size()) {
string s(dictNew, a + 1, dictNew.find(',', a + 1) - a - 1);
string key(s, 0, s.find(':', 0));
string value(s, s.find(':', 0) + 1, s.size() - s.find(':', 0));
if (ma.find(key) == ma.end()) {
ad[addCount++] = key;
}
else {
string temp = ma.find(key)->second;
if (temp != value) {
cha[changeCount++] = key;
}
ma.erase(key);
}
a = dictNew.find(',', a + 1);
}
string s1(dictNew, a + 1, dictNew.find('}', a + 1) - a - 1);
string key1(s1, 0, s1.find(':', 0));
string value1(s1, s1.find(':', 0) + 1, s1.size() - s1.find(':', 0));
if (key1.size()) {
if (ma.find(key1) == ma.end()) {
ad[addCount++] = key1;
}
else {
string temp = ma.find(key1)->second;
if (temp != value1) {
cha[changeCount++] = key1;
}
ma.erase(key1);
}
}
while (ma.begin() != ma.end()) {
string temp = ma.begin()->first;
del[deleteCount++] = temp;
ma.erase(temp);
}
if (addCount == 0 &&
deleteCount == 0 &&
changeCount == 0) {
cout << "No changes\n";
}
else {
if (addCount) {
sort(ad, ad + addCount);
cout << "+";
for (int i = 0; i < addCount; i++) {
if (i) {
cout << ",";
}
cout << ad[i];
}
cout << endl;
}
if (deleteCount) {
sort(del, del + deleteCount);
cout << "-";
for (int i = 0; i < deleteCount; i++) {
if (i) {
cout << ",";
}
cout << del[i];
}
cout << endl;
}
if (changeCount) {
sort(cha, cha + changeCount);
cout << "*";
for (int i = 0; i < changeCount; i++) {
if (i) {
cout << ",";
}
cout << cha[i];
}
cout << endl;
}
}
cout << endl;
}
return 0;
}
Uva - 12504 - 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(STL)
题目 题目 分析 第一次用stringstream,真TMD的好用 代码 #include <bits/stdc++.h> using namespace std; int ...
- Uva 511 Updating a Dictionary
大致题意:用{ key:value, key:value, key:value }的形式表示一个字典key表示建,在一个字典内没有重复,value则可能重复 题目输入两个字典,如{a:3,b:4,c: ...
- 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)处理旧字典, ...
- [刷题]算法竞赛入门经典(第2版) 5-11/UVa12504 - Updating a Dictionary
题意:对比新老字典的区别:内容多了.少了还是修改了. 代码:(Accepted,0.000s) //UVa12504 - Updating a Dictionary //#define _XieNao ...
- Problem C Updating a Dictionary
Problem C Updating a Dictionary In this problem, a dictionary is collection of key-value pairs, ...
- Updating a Dictionary UVA - 12504
In this problem, a dictionary is collection of key-value pairs, where keys are lower-case letters, a ...
- 【习题 5-11 UVA 12504 】Updating a Dictionary
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 不确定某个map里面是否有某个关键字的时候. 要用find来确定. 如果直接用访问下标的形式去做的话. 会强行给他加一个那个关键字( ...
随机推荐
- [Luogu 1516] 青蛙的约会
Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事 ...
- JavaScript 题目(作用域)
var length = 10 function fn(){ alert(this.length) } var obj = { length: 5, method: function(fn) { fn ...
- 补充Mysql5.7用法
下面简单介绍一下安装: [root@MySQL soft]# tar xf mysql-5.7.10-linux-glibc2.5-x86_64.tar.gz -C /data/service/ [r ...
- Angular5 路由传参的3种方法
一共3种方法. 1.问号后面带的参数,获取参数的方式:ActivatedRoute.queryParams[id] 例如:/product?id=1&name=iphone还可以是: [rou ...
- FJUT第四周寒假作业之第一集,临时特工?(深度优先搜索)
原网址:http://210.34.193.66:8080/vj/Contest.jsp?cid=163#P2 第一集,临时特工? TimeLimit:1000MS MemoryLimit:128M ...
- PHP MySQL Update
UPDATE 语句用于中修改数据库表中的数据. 更新数据库中的数据 UPDATE 语句用于更新数据库表中已存在的记录. 语法 UPDATE table_name SET column1=value, ...
- Bootstrap3 代码-变量
通过 <var> 标签标记变量. y = mx + b <var>y</var> = <var>m</var><var>x< ...
- User-Agent-Switcher和fiddler
浏览器模拟器(可以模拟各种浏览器效果,浏览器中看手机显示的效果) http://chromecj.com/web-development/2014-09/70.html User-Agent-Swit ...
- android launcher 之踩到的坑
需求: 1. 用android系统launcher 隐藏主菜单 所有应用显示在桌面 即workspace上: 2.隐藏launcher上方默认的google search: 3.切换一套launche ...
- Android TV开发总结(六)构建一个TV app的直播节目实例
请尊重分享成果,转载请注明出处:http://blog.csdn.net/hejjunlin/article/details/52966319 近年来,Android TV的迅速发展,传统的有线电视受 ...