http://blog.csdn.net/zyq522376829/article/details/46484575
错误代码:
#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <map>
#include <set>
using namespace std;
const int MAXN = 100 + 5;
int main(){
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
map<string, string> ma;
string dictOld, dictNew;
string add[MAXN], del[MAXN], cha[MAXN];
int addCount = 0;
int delCount = 0;
int chaCount = 0;
cin >> dictOld >> dictNew;
int a = 0;
while (dictOld.find(',', a + 1) < dictOld.size()) {
//截取
string s(dictOld, a + 1, dictOld.find(',', a + 1) - a - 1);
string key(s, 0, s.find(':', 0));
string value(s, s.find(':', 0) + 1, s.size() - s.find(':', 0));
ma.insert(pair<string, string>(key, value));
a = dictOld.find(',', a + 1);
}
string s(dictOld, a + 1, dictOld.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 (key.size()) {
ma.insert(pair<string, string>(key, 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()) {
add[addCount++] = key;
}
else {
string temp = ma.find(key)->second;
if (temp != value) {
cha[chaCount++] = key;
}
ma.erase(key);
//erase为了之后得到del
}
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()) {
add[addCount++] = key1;
}
else {
string temp = ma.find(key1)->second;
if (temp != value1) {
cha[chaCount++] = key1;
}
ma.erase(key1);
}
}
while (ma.begin() != ma.end()) {
string temp = ma.begin()->first;
del[delCount++] = temp;
ma.erase(key1);
}
if (addCount == 0 && delCount == 0 && chaCount == 0) {
cout << "No changes" << endl;
}
else {
if (addCount) {
sort(add, add + addCount);
cout << "+";
for (int i = 0; i < addCount; i++) {
if (i) {
cout << ",";
}
cout << add[i];
}
cout << endl;
}
if (delCount) {
sort(del, del + delCount);
cout << "-";
for (int i = 0; i < delCount; i++) {
if (i) {
cout << ",";
}
cout << del[i];
}
cout << endl;
}
if (chaCount) {
sort(cha, cha + chaCount);
cout << "*";
for (int i = 0; i < chaCount; i++) {
if (i) {
cout << ",";
}
cout << cha[i];
}
cout << endl;
}
}
cout << endl;
}
return 0;
}