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来确定. 如果直接用访问下标的形式去做的话. 会强行给他加一个那个关键字( ...
随机推荐
- 在vue生命周期中及时销毁全局作用的代码
一.纯客户端中 对于全局的代码,比如定时器等,在 beforeDestroy或 destroyed 生命周期时将其销毁.如果在跳转路由时候,组件销毁了,全局的定时器却没有销毁,这会使得页面产生卡顿. ...
- Android项目实战(四十五):Usb转串口通讯(CH34xUARTDriver)
需求为:手机usb接口插入一个硬件,从硬件上获取数据 例如:手机usb插入硬件A,A通过蓝牙通讯获取设备a.b的数据,作为中转站(可以做些数据处理)将数据(设备a.b产生的)传给手机程序. 设备A也可 ...
- 服务器&阵列卡&组raid 5
清除raid信息后,机器将会读不到系统, 后面若进一步操作处理, raid信息有可能会被初始化掉,那么硬盘数据就有可能会被清空, 导致数据丢失, 否则如果只是清除raid信息,重做raid是可以还原系 ...
- 02_Action
1.action VS Action action:代表一个Struts2的请求 Action:能够处理action请求的类 属性名必须与JavaBeans属性名相同 属性的类型可以是任意类型,从字符 ...
- Do a web framework ourselves
step 1: from wsgiref.simple_server import make_server def application(environ, start_response): star ...
- [多线程] 生产者消费者模型的BOOST实现
说明 如果 使用过程中有BUG 一定要告诉我:在下面留言或者给我邮件(sawpara at 126 dot com) 使用boost::thread库来实现生产者消费者模型中的缓冲区! 仓库内最多可以 ...
- java.io.FileNotFoundException: D:\Program%20Files\Apache%20Software%20Foundation\Tomcat%205.0\webapp
慢慢把以前遇到过的问题一点点发出来,以前做的笔记比较杂: java.io.FileNotFoundException: D:\Program%20Files\Apache%20Software%20F ...
- Linux下端口复用(SO_REUSEADDR与SO_REUSEPORT)
freebsd与linux下bind系统调用小结: 只考虑AF_INET的情况(同一端口指ip地址与端口号都相同) freebsd支持SO_REUSEPORT和SO_REUSEADDR选项,而l ...
- 代码优化>>>Android ListView适配器三级优化详解
转载本专栏每一篇博客请注明转载出处地址,尊重原创.此博客转载链接地址:点击打开链接 http://blog.csdn.net/qq_32059827/article/details/52718489 ...
- 从Cell类型转变成数据型
我们有一个如下的cell数据 cdata = {'1' '11' '111' '1111' '11111'}; 现在要把他转变成double型的数组,很自然会想到的方法是cell2mat,可悲的是会遇 ...