Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <string>
  6. #include <map>
  7. #include <set>
  8. using namespace std;
  9. typedef long long ll;
  10.  
  11. class Person {
  12. public:
  13.     void ChangeFirstName(int year, const string& first_name) {
  14.         // добавить факт изменения имени на first_name в год year
  15.         f_name[year] = first_name;
  16.     }
  17.     void ChangeLastName(int year, const string& last_name) {
  18.         // добавить факт изменения фамилии на last_name в год year
  19.         s_name[year] = last_name;
  20.     }
  21.  
  22.     string GetName(const map<int, string> &dict, int year) {
  23.         string res;
  24.         vector<string> ss;
  25.         vector<string> s;
  26.  
  27.         for (auto it = dict.rbegin(); it != dict.rend(); ++it)
  28.             if (it->first <= year)
  29.                 ss.push_back(it->second);
  30.  
  31.         if (!ss.empty()) {
  32.             s.push_back(ss[0]);
  33.             res += s[0];
  34.         }
  35.         for (int i = 1; i < ss.size(); ++i)
  36.             if (ss[i] != ss[i - 1])
  37.                 s.push_back(ss[i]);
  38.  
  39.         if (s.size() > 1) {
  40.             res += " (";
  41.             for (int i = 1; i < s.size(); ++i)
  42.                 if (!i || s[i] != s[i - 1]) {
  43.                     res += s[i];
  44.                     if (s.size() > 2 && i < s.size() - 1)
  45.                         res += ", ";
  46.                 }
  47.             res += ")";
  48.         }
  49.  
  50.         return res;
  51.     }
  52.  
  53.     string GetFullName(int year) {
  54.         // получить имя и фамилию по состоянию на конец года year
  55.         string res;
  56.         for (auto it = f_name.rbegin(); it != f_name.rend(); ++it)
  57.             if (it->first <= year) {res += it->second; break;}
  58.         res += " ";
  59.         for (auto it = s_name.rbegin(); it != s_name.rend(); ++it)
  60.             if (it->first <= year) {res += it->second; break;}
  61.         if (res == " ")
  62.             res = "Incognito";
  63.         else if (res[0] == ' ') {
  64.             res.erase(0, 1);
  65.             res += " with unknown first name";
  66.         }
  67.         else if (res[res.size() - 1] == ' ')
  68.             res += "with unknown last name";
  69.         return res;
  70.     }
  71.  
  72.     string GetFullNameWithHistory(int year) {
  73.         string res;
  74.         res += GetName(f_name, year);
  75.         res += " ";
  76.         res += GetName(s_name, year);
  77.         if (res == " ")
  78.             res = "Incognito";
  79.         else if (res[0] == ' ') {
  80.             res.erase(0, 1);
  81.             res += " with unknown first name";
  82.         }
  83.         else if (res[res.size() - 1] == ' ')
  84.             res += "with unknown last name";
  85.         return res;
  86.     }
  87.  
  88. private:
  89.     // приватные поля
  90.     map<int, string> f_name;
  91.     map<int, string> s_name;
  92. };
  93.  
  94. /*int main() {
  95.     Person person;
  96.  
  97.     person.ChangeFirstName(1900, "Eugene");
  98.     person.ChangeLastName(1900, "Sokolov");
  99.     person.ChangeLastName(1910, "Sokolov");
  100.     person.ChangeFirstName(1920, "Evgeny");
  101.     person.ChangeLastName(1930, "Sokolov");
  102.     cout << person.GetFullNameWithHistory(1940) << endl;
  103.  
  104.     return 0;
  105. }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement