UVA12504 Updating a Dictionary

xiaoxiao2021-02-28  100

#include<iostream> #include<vector> #include<string> #include<type_traits> #include<sstream> #include<tuple> #include<bitset> #include<regex> #include<set> #include<stack> #include<queue> #include<map> using namespace std; int T; void Explain(map<string,string>& str2str,string source){ vector<int> index1, index2; for (int i = 0; i < source.size(); i++){ if (source[i] == ':') index1.push_back(i); if (source[i] == ',') index2.push_back(i); } index2.push_back(-1); int pos1 = 0; for (int i = 0; i < index1.size(); i++){ string key = source.substr(pos1,index1[i]-pos1); string value; if (index2[i] != -1){ value = source.substr(index1[i] + 1, index2[i] - index1[i] - 1); pos1 = index2[i] + 1; } else{ value = source.substr(index1[i]+1); } str2str[key] = value; } } int main(){ cin >> T; string del; getline(cin,del); while (T--){ string s1; string s2; getline(cin,s1); getline(cin,s2); s1 = s1.substr(1,s1.size()-2); s2 = s2.substr(1,s2.size()-2); map<string, string> ms1; map<string, string> ms2; Explain(ms1, s1); Explain(ms2,s2); vector<string> Add; vector<string> Remove; vector<string> Change; auto it1 = ms1.begin(); auto it2 = ms2.begin(); while (it1 != ms1.end() && it2 != ms2.end()){ string key1 = it1->first; string value1 = it1->second; string key2 = it2->first; string value2 = it2->second; if (key1 == key2){ if (value1 != value2){ Change.push_back(key1); } it1++; it2++; } else{ if (ms2.find(key1) == ms2.end()){ Remove.push_back(key1); it1++; } if (ms1.find(key2) == ms1.end()){ Add.push_back(key2); it2++; } } } while (it1 != ms1.end()){ Remove.push_back(it1->first); it1++; } while (it2 != ms2.end()){ Add.push_back(it2->first); it2++; } bool flag = false; if (Add.size()){ cout << "+"; flag = true; for (int i = 0; i < Add.size(); i++){ cout << Add[i]; if (i < Add.size() - 1) cout << ','; } cout << endl; } if (Remove.size()){ flag = true; cout << "-"; for (int i = 0; i < Remove.size(); i++){ cout << Remove[i]; if (i < Remove.size() - 1) cout << ","; } cout << endl; } if (Change.size()){ flag = true; cout << "*"; for (int i = 0; i < Change.size(); i++){ cout << Change[i]; if (i < Change.size() - 1) cout << ","; } cout << endl; } if (!flag){ cout << "No changes" << endl; } cout << endl; } //system("pause"); return 0; }
转载请注明原文地址: https://www.6miu.com/read-18459.html

最新回复(0)