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

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. Windows Server 2008 R2服务器系统安全设置参考指南

    Server 2008 R2服务器系统安全设置参考指南  重点比较重要的几部 1.更改默认administrator用户名,复杂密码 2.开启防火墙 3.安装杀毒软件 1)新做系统一定要先打上补丁(升 ...

  2. 如何成为快手尬舞王?HUAWEI HiAI了解一下!

    左手!右手!抱一抱!扭一扭! 快手短视频,红遍东西南北中, 给大家的生活增添了不少乐趣. 有了人体姿态识别的魔法表情, 不会跳舞的也都可以跟着跳一跳. 从村口朴实的阿姨,到写字楼里端庄的白领, 在人体 ...

  3. java对redis的操作

    需要两个包的支持 jedis-2.1.0.jar commons-pool-1.5.4.jar 一个连接池一个工具类 pool代码 public class RedisUtil { private s ...

  4. Linux的发行版,不同发行版之间的联系和区别

    Linux 主要作为Linux发行版(通常被称为"distro")的一部分而使用.这些发行版由个人,松散组织的团队,以及商业机构和志愿者组织编写.它们通常包括了其他的系统软件和应用 ...

  5. Linux文件管理笔记

    1)Linux识别磁盘:Linux通过不同的设备节点区分各个分区,节点名字的由磁盘名加分区号组成.例如,驱动器/dev/hba上的第一个分区叫做/dev/hba1,驱动器/dev/sdc上的第七个分区 ...

  6. iis部署python运行环境

    IIS部署 1.启用或者关闭windows功能,选择安装CGI,我这里已经安装过了. 2.安装后重新打开IIS看到CGI 3.配置ISAPI和CGI限制 4.右上角添加,路径是python安装路径,注 ...

  7. Bootstrap3 表格-条纹状表格

    通过 .table-striped 类可以给 之内的每一行增加斑马条纹样式. 跨浏览器兼容性 条纹状表格是依赖 :nth-child CSS 选择器实现的,而这一功能不被 Internet Explo ...

  8. Matlab—regexp正则表达式

    原文转自:http://blog.csdn.net/yf210yf/article/details/42421523 关于正则表达式的基本知识 正则表达式就是一个表达式(也是一串字符),它定义了某种字 ...

  9. pg备份恢复与设置编码

    psql create database jasygl encoding='UTF8' TEMPLATE = template0; 命令行 备份 pg_dump dabase_name > ba ...

  10. RDO Stack Exception: UnboundLocalError: local variable 'logFile' referenced before assignment

    Issue: When you install RDO stack on CentOS, you may encounter following error. Error: [root@localho ...