全是字符串相关处理,截取长度等相关操作的练习

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的更多相关文章

  1. [ACM_模拟] UVA 12504 Updating a Dictionary [字符串处理 字典增加、减少、改变问题]

      Updating a Dictionary  In this problem, a dictionary is collection of key-value pairs, where keys ...

  2. 【UVA】12504 Updating a Dictionary(STL)

    题目 题目     分析 第一次用stringstream,真TMD的好用     代码 #include <bits/stdc++.h> using namespace std; int ...

  3. Uva 511 Updating a Dictionary

    大致题意:用{ key:value, key:value, key:value }的形式表示一个字典key表示建,在一个字典内没有重复,value则可能重复 题目输入两个字典,如{a:3,b:4,c: ...

  4. csuoj 1113: Updating a Dictionary

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1113 1113: Updating a Dictionary Time Limit: 1 Sec  ...

  5. 湖南生第八届大学生程序设计大赛原题 C-Updating a Dictionary(UVA12504 - Updating a Dictionary)

    UVA12504 - Updating a Dictionary 给出两个字符串,以相同的格式表示原字典和更新后的字典.要求找出新字典和旧字典的不同,以规定的格式输出. 算法操作: (1)处理旧字典, ...

  6. [刷题]算法竞赛入门经典(第2版) 5-11/UVa12504 - Updating a Dictionary

    题意:对比新老字典的区别:内容多了.少了还是修改了. 代码:(Accepted,0.000s) //UVa12504 - Updating a Dictionary //#define _XieNao ...

  7. Problem C Updating a Dictionary

    Problem C     Updating a Dictionary In this problem, a dictionary is collection of key-value pairs, ...

  8. Updating a Dictionary UVA - 12504

    In this problem, a dictionary is collection of key-value pairs, where keys are lower-case letters, a ...

  9. 【习题 5-11 UVA 12504 】Updating a Dictionary

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 不确定某个map里面是否有某个关键字的时候. 要用find来确定. 如果直接用访问下标的形式去做的话. 会强行给他加一个那个关键字( ...

随机推荐

  1. java Session统计在线用户,并且显示在线用户

    关键字: httpsession 1.http://www.jspcn.net/htmlnews/11049329478121583.html       监听器 2.session.invalida ...

  2. Go 实现字符串相似度计算函数 Levenshtein 和 SimilarText

    [转]http://www.syyong.com/Go/Go-implements-the-string-similarity-calculation-function-Levenshtein-and ...

  3. Java第3次实验提纲(面向对象1-基本概念)

    0. 将码云的项目clone到本机 请参考使用Eclipse Egit与码云管理你的代码中的3 从码云将项目clone到你的电脑 之后就可以在Eclipse中提交本地项目新增或修改的文件.如果在Ecl ...

  4. 《读书报告 -- Elasticsearch入门 》--简单使用(2)

    <读书报告 – Elasticsearch入门 > ' 第四章 分布式文件存储 这章的主要内容是理解数据如何在分布式系统中存储. 4.1 路由文档到分片 创建一个新文档时,它是如何确定应该 ...

  5. Dubbo框架应用之(一)--服务体系

    Dubbo 是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和 Spring框架无缝集成,也是一个非常全面的SOA基础框架.其是阿里巴巴SO ...

  6. Android存储之SQLite数据库

    Android存储之SQLite数据库数据库 创建数据库 package --; import android.content.Context; import android.database.sql ...

  7. mxgraph进阶(四)mxGraph再启程

    mxgraph进阶(四)mxGraph再启程 前言   小论文Constructing User Interaction Behaviors Net from System Log. (AICE 20 ...

  8. ROS机器人程序设计(原书第2版)补充资料 教学大纲

    ROS机器人程序设计(原书第2版) 补充资料 教学大纲 针对该书稍后会补充教学大纲.教案.多媒体课件以及练习题等. <ROS机器人程序设计>课程简介 课程编号:XXXXXX 课程名称:RO ...

  9. 【完整的App项目】颖火虫笔记

    这是本人花大概一个星期开发出来的一款App,这是一款类似印象笔记的App,随时记录您的生活点滴.首先说一下自己为何要开发这款App,因为自己手机系统自带的笔记应用功能太low,界面不够漂亮,所以自己就 ...

  10. RxJava(九)zip操作符在Android中的实际使用场景

    欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/51614927 本文出自:[余志强的博客] 一.zip操作符概述 官方 ...