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

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. JButton

    JButton和Button区别: Button是在java.awt.*中的,而JButton是在javax.swing.*中,swing是awt的一个扩展,由纯java便携,它有一个与平台无关的实现 ...

  2. React框架 dva 和 mobx 的使用感受

    最近在用react写web项目,领导为了让前端便于维护要求都用react作为开发基础,框架选型不限.在使用 react 的时候或多或少会接触到状态管理,从开始学 react 到现在也挺久了,做一些前端 ...

  3. Kafka生产者-向Kafka中写入数据

    (1)生产者概览 (1)不同的应用场景对消息有不同的需求,即是否允许消息丢失.重复.延迟以及吞吐量的要求.不同场景对Kafka生产者的API使用和配置会有直接的影响. 例子1:信用卡事务处理系统,不允 ...

  4. 再见,segmentfault

    再见,segmentfault 太多的Bug,对segmentfault已经爱不起了. 重回博客园~

  5. ACM Primes

    Write a program to read in a list of integers and determine whether or not each number is prime. A n ...

  6. ubuntu16.04下安装opencv

    source url:http://blog.csdn.net/zhuiqiuk/article/details/5308505811 1 依赖包sudo apt-get install build- ...

  7. Android Material Design(一)史上最全的材料设计控件大全

    主要内容: 本文将要介绍Material design和Support library控件,主要包括TextInputLayout.SwitchCompat.SnackBar.FloatingActi ...

  8. WebView 的使用案例

    package com.example.day20_webview; import android.os.Bundle; import android.annotation.SuppressLint; ...

  9. iOS界面不能点击(tableView 的cell 不能使用点击事件,tableView也不能上下滚动)

    iOS开发中间遇到了很多问题,有些后来又遇到,竟然忘记怎么处理了,所以还是来写下博客记录自己遇到的问题,方便自己也方便别人. 之前想的是项目小,就用storyboard在绘制界面,但是项目慢慢的扩展, ...

  10. [apache2.4]configure: error: APR not found. Please read the documentation.

    apache2.4 安装出现如下错误 ``` [lzz@localhost httpd-2.4.10]$ ./configure  checking for chosen layout... Apac ...