Advertisement
georgiy110802

Untitled

Sep 14th, 2021
1,003
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     string s;
  7.     getline(cin, s);
  8.     map<string, int> count;
  9.     for (auto i : s)
  10.         count[string(1, i)]++;
  11.  
  12.     set<pair<int, string>> st;
  13.     for (auto &[_string, _int] : count) {
  14.         st.insert({_int, _string});
  15.     }
  16.     map<char, string> code;
  17.     for (int i = 0; i < count.size() - 1; ++i) {
  18.         auto [cnt1, letters1] = *st.begin();
  19.         st.erase(st.begin());
  20.         auto [cnt2, letters2] = *st.begin();
  21.         st.erase(st.begin());
  22.         for (auto c : letters1) {
  23.             code[c] = "0" + code[c];
  24.         }
  25.         for (auto c : letters2) {
  26.             code[c] = "1" + code[c];
  27.         }
  28.         st.insert({cnt1 + cnt2, letters1 + letters2});
  29.     }
  30.     for (auto& [c, s] : code) {
  31.         cout << c << " " << s << "\n";
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement