Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <algorithm>
- #include <vector>
- #include <map>
- using namespace std;
- class Person {
- public:
- void ChangeFirstName(int year, const string &firstName) {
- firstNames[year] = firstName;
- }
- void ChangeLastName(int year, const string &lastName) {
- lastNames[year] = lastName;
- }
- string GetFullName(int year) const {
- return FirstAndLastNameToText(GetLatestName(year, firstNames),
- GetLatestName(year, lastNames));
- }
- string GetFullNameWithHistory(int year) const {
- vector<string> firstNamesChain = GetUniqueChain(year, firstNames);
- vector<string> lastNamesChain = GetUniqueChain(year, lastNames);
- string firstName = GetNameFromUniqueChain(firstNamesChain);
- string lastName = GetNameFromUniqueChain(lastNamesChain);
- return FirstAndLastNameToText(firstName, lastName);
- }
- private:
- map<int, string> firstNames;
- map<int, string> lastNames;
- string FirstAndLastNameToText(const string &firstName, const string &lastName) const {
- if (firstName.empty() && lastName.empty())
- return "Incognito";
- if (firstName.empty())
- return lastName + " with unknown first name";
- if (lastName.empty())
- return firstName + " with unknown last name";
- return firstName + " " + lastName;
- }
- string GetLatestName(const int &year, const map<int, string> &names) const {
- string nameString = "";
- for(const auto &name : names)
- if (name.first > year)
- break;
- else
- nameString = name.second;
- return nameString;
- }
- vector<string> GetUniqueChain(const int &year, const map<int, string> &names) const {
- vector<string> uniqueChain;
- for (const auto &[nameYear, name] : names)
- if (nameYear > year)
- break;
- else if (uniqueChain.empty() || name != uniqueChain.back())
- uniqueChain.push_back(name);
- return uniqueChain;
- }
- string GetNameFromUniqueChain(const vector<string> &uniqueChain) const {
- switch(uniqueChain.size()) {
- case 0:
- return "";
- case 1:
- return uniqueChain[0];
- default:
- string name = uniqueChain.back() + " (" + *(uniqueChain.end() - 2);
- for(int i = uniqueChain.size() - 3; i >= 0; i--)
- name += ", " + uniqueChain[i];
- return name + ")";
- }
- }
- };
Add Comment
Please, Sign In to add comment