Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- class Person {
- private:
- map<int, string> fn;
- map<int, string> sn;
- string FirstName(const int &year) {
- string first_name = " with unknown first name";
- for (const pair<int, string> &p : fn) {
- if (p.first <= year)
- first_name = p.second;
- else
- break;
- }
- return first_name;
- }
- string LastName(const int &year) {
- string last_name = "with unknown last name";
- for (const pair<int, string> &p : sn) {
- if (p.first <= year)
- last_name = p.second;
- else
- break;
- }
- return last_name;
- }
- bool FirstNameHistoryExists(const int &year){
- int cnt = 0;
- for (const auto &p : fn) {
- if (p.first > year) break;
- else cnt++;
- }
- if (cnt >= 2) return true;
- else return false;
- }
- bool LastNameHistoryExists(const int &year) {
- int cnt = 0;
- for (const auto &p : sn) {
- if (p.first > year)
- break;
- else
- cnt++;
- }
- if (cnt >= 2)
- return true;
- else
- return false;
- }
- void FillFirstNames(vector<string> &v, const int &year) {
- for (const pair<int, string> &p : fn) {
- if (p.first <= year)
- v.push_back(p.second);
- else
- break;
- }
- reverse(begin(v), end(v));
- v.erase(unique(begin(v), end(v)), end(v));
- }
- void FillLastNames(vector<string> &v, const int &year) {
- for (const pair<int, string> &p : sn) {
- if (p.first <= year)
- v.push_back(p.second);
- else
- break;
- }
- reverse(begin(v), end(v));
- v.erase(unique(begin(v), end(v)), end(v));
- }
- public:
- void ChangeFirstName(const int &year, const string &first_name) {
- fn[year] = first_name;
- }
- void ChangeLastName(const int &year, const string &last_name) {
- sn[year] = last_name;
- }
- string GetFullName(const int &year) {
- if (FirstName(year) == " with unknown first name" && LastName(year) == "with unknown last name")
- return "Incognito";
- if (FirstName(year) == " with unknown first name")
- return LastName(year) + FirstName(year);
- return FirstName(year) + ' ' + LastName(year);
- }
- string GetFullNameWithHistory(const int &year) {
- string s = GetFullName(year);
- if (s == "Incognito")
- return "Incognito";
- if (FirstName(year) == " with unknown first name") {
- if (!LastNameHistoryExists(year))
- return LastName(year) + " with unknown first name";
- vector<string> v;
- FillLastNames(v, year);
- s = v[0] + " (" + v[1];
- for (int i = 2; i < v.size(); i++) {
- s += ", " + v[i];
- }
- s += ") with unknown first name";
- return s;
- }
- if (LastName(year) == "with unknown last name") {
- if (!FirstNameHistoryExists(year))
- return FirstName(year) + " with unknown last name";
- vector<string> v;
- FillFirstNames(v, year);
- s = v[0] + " (" + v[1];
- for (int i = 2; i < v.size(); i++) {
- s += ", " + v[i];
- }
- s += ") with unknown last name";
- return s;
- }
- vector<string> v;
- if (FirstNameHistoryExists(year)) {
- FillFirstNames(v, year);
- s = v[0] + " (" + v[1];
- for (int i = 2; i < v.size(); i++) {
- s += ", " + v[i];
- }
- s += ") ";
- } else
- s = FirstName(year) + ' ';
- v.clear();
- if (LastNameHistoryExists(year)) {
- FillLastNames(v, year);
- s += v[0] + " (" + v[1];
- for (int i = 2; i < v.size(); i++) {
- s += ", " + v[i];
- }
- s += ")";
- } else
- s += LastName(year);
- return s;
- }
- };
- int main() {
- Person person;
- person.ChangeFirstName(1965, "Polina");
- person.ChangeLastName(1967, "Sergeeva");
- for (int year : {1900, 1965, 1990}) {
- cout << person.GetFullNameWithHistory(year) << endl;
- }
- person.ChangeFirstName(1970, "Appolinaria");
- for (int year : {1969, 1970}) {
- cout << person.GetFullNameWithHistory(year) << endl;
- }
- person.ChangeLastName(1968, "Volkova");
- for (int year : {1969, 1970}) {
- cout << person.GetFullNameWithHistory(year) << endl;
- }
- person.ChangeFirstName(1990, "Polina");
- person.ChangeLastName(1990, "Volkova-Sergeeva");
- cout << person.GetFullNameWithHistory(1990) << endl;
- person.ChangeFirstName(1966, "Pauline");
- cout << person.GetFullNameWithHistory(1966) << endl;
- person.ChangeLastName(1960, "Sergeeva");
- for (int year : {1960, 1967}) {
- cout << person.GetFullNameWithHistory(year) << endl;
- }
- person.ChangeLastName(1961, "Ivanova");
- cout << person.GetFullNameWithHistory(1967) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement