Originally a very simple topic, but because of a variety of details, wrong two times.
The text is not said, the original is very good understanding, but note that the output according to the dictionary output, because their English is not good (T T)lexicographically This word automatically ignored him, in fact, this is in accordance with the meaning of the dictionary order output. Hey.
Overall idea:
1. Input
2. The establishment of two maps is a new and old dictionary, in the establishment of three vector<string>out[3] is the record of the last three results, the establishment of two Vector<string>cpe_new,cpe_old respectively record the contents of the map. Of course, you can also directly traverse the contents of the map. (is the iterator variable it traversal), the last one by one contrast processing can be.
Skills:
1. Regarding the input, may use the StringStream input, first getline, the comma and the braces automatically becomes the space, enters in, the use case question 5-11 method. Find Colon: Separated into two parts, as the key and key values of the map
Attention:
1. For getline, pay attention to GetChar () Eat line. Front scanf to eat one, and for my method is to write the input into 1 functions two calls, so not every getline to add GetChar (), but only the first to eat line. So, add GetChar () only after the first call;
2. Dictionary output of the words directly to the output of the sort can be sorted (Out[i].begin (), Out[i].end ());
The code is as follows:
#include <map> #include <iostream> #include <sstream> #include <string> #include <vector>
#include <cstdio> #include <cstring> #include <algorithm> using namespace std;
map<string,string>old,now;
vector<string>out[5],cpe_old,cpe_new;
Const string Flag[5] = {"+", "-", "*"};
void Clear_all () {old.clear (); Now.clear ();
Out[0].clear (); Out[1].clear (); Out[2].clear ();
Cpe_old.clear (); Cpe_new.clear ();
} void Deal (const string&str,string &before,string &after) {int k = Str.find (":");
Before=str.substr (0,K);
After=str.substr (k+1);
} void input (int way) {string str,code,key,value;
Getline (CIN,STR); for (int i = 0; i < str.size (); ++i) if (str[i] = = ', ' | | str[i]== ' {' | |
Str[i] = = = '} ' str[i] = ';
StringStream SS (str);
while (Ss>>code) {deal (code,key,value);
if (way) {cpe_new.push_back (key); now[key]=value;}
else {cpe_old.push_back (key); old[key]=value;} } if (!Way) GetChar ();
void Compare () {for (int i = 0; i < cpe_new.size (); ++i) {string code=cpe_new[i];
if (!old.count (code)) Out[0].push_back (code);
if (Old.count (code) && Old[code]!= Now[code]) out[2].push_back (code);
for (int i = 0; i < cpe_old.size (); ++i) {string code=cpe_old[i];
if (!now.count (code)) Out[1].push_back (code);
} void Print () {if (!out[0].size () &&!out[1].size () &&!out[2].size ()) printf ("No changes\n"); else {for (int i = 0; i < 3; ++i) if ((int) out[i].size ()) {sort (out[i].begin), out[
I].end ());
cout << Flag[i] << out[i][0];
for (int k = 1; k < out[i].size (); ++k) cout<< "," <<out[i][k];
printf ("\ n");
int main () {//freopen ("OUT.txt", "w", stdout);
int n;
scanf ("%d", &n);
GetChar ();
while (n--) {clear_all (); InpUT (0);//old input (1);//new compare ();
Print ();
printf ("\ n");
return 0;
}