Advertisement
RobertDeMilo

RB2.12 Практические применения

Apr 16th, 2024
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3. #include<map>
  4. #include<set>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     int q;
  11.     cin >> q;
  12.  
  13.     map<string, set<string>> synonyms;
  14.  
  15.     for (int i = 0; i < q; i++)   // Q итераций
  16.     {
  17.         string operation_code;
  18.         cin >> operation_code;
  19.  
  20.         if (operation_code == "ADD")
  21.         {
  22.             string first_word, second_word;
  23.             cin >> first_word >> second_word;   // L
  24.  
  25.             synonyms[first_word].insert(second_word);  // LlogQ
  26.             synonyms[second_word].insert(first_word);
  27.             // O(LlogQ)
  28.         }
  29.         else if (operation_code == "COUNT")
  30.         {
  31.             string word;
  32.             cin >> word;
  33.  
  34.             cout << synonyms[word].size()<<endl;
  35.             // O(LlogQ)
  36.         }
  37.         else if (operation_code == "CHECK")
  38.         {
  39.             string first_word, second_word;
  40.             cin >> first_word >> second_word;
  41.  
  42.             if (synonyms[first_word].count(second_word)==1)
  43.             {
  44.                 cout << "YES" << endl;
  45.             }
  46.             else
  47.             {
  48.                 cout << "NO" << endl;
  49.             }
  50.             // O(LlogQ)
  51.         }
  52.  
  53.         // O(QLlogQ)
  54.     }
  55.  
  56.     return 0;
  57. }
  58. ////////////////////////////////////////////////////////////////////////
  59. #include <iostream>
  60. #include <string>
  61. #include <map>
  62.  
  63. using namespace std;
  64.  
  65. static string FindNameByYear(const map<int,string>&names, int year)
  66. {
  67.     string name;
  68.    
  69.     for(const auto& item:names) //O(Q)
  70.     {
  71.         if(item.first<=year)    //O(1)
  72.         {
  73.             name=item.second;   //O(L)
  74.         }
  75.         else
  76.         {
  77.             break;
  78.         }
  79.     }
  80.     return name;
  81.     // O(QL)
  82. }
  83.  
  84. static string FindNameByYear2(const map<int, string>&names, int year)
  85. {
  86.     auto iter_after = names.upper_bound(year);
  87.  
  88.     string name;
  89.  
  90.     if(iter_after!=names.begin())
  91.     {
  92.         name =(--iter_after)->second;
  93.     }
  94.     return name;
  95.     // O(LogQ + L)
  96. }
  97.  
  98. class Person
  99. {
  100. public:
  101.    
  102.     void ChangeFirstName(int year, const string& first_name)
  103.     {
  104.         first_names[year]= first_name;
  105.         // Q(LogQ+L)
  106.     }
  107.    
  108.     void ChangeLastName(int year, const string& last_name)
  109.     {
  110.         last_names[year]= last_name;
  111.         // Q(LogQ+L)
  112.     }
  113.    
  114.     string GetFullName(int year)
  115.     {
  116.         const string first_name = FindNameByYear(first_names,year);
  117.         const string last_name = FindNameByYear(last_names,year);
  118.  
  119.         if(first_name.empty()&&last_name.empty())
  120.         {
  121.             return "Incognito";
  122.         }
  123.         else if(first_name.empty())
  124.         {
  125.             return last_name+ " with unknown first name";
  126.         }
  127.         else if(last_name.empty())
  128.         {
  129.             return first_name+ " with unknown last name";
  130.         }
  131.         else
  132.         {
  133.             return first_name + " " + last_name;
  134.         }
  135.  
  136.         // O(L) + O(QL) = O(QL)
  137.         // O(LogQ + L)
  138.     }
  139.    
  140. private:
  141.     map<int,string>first_names;
  142.     map<int,string>last_names;
  143. };
  144. // O(Q^2 * L)
  145. // O(Q * (L + LogQ))
  146.  
  147. int main()
  148. {
  149.     return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement