Advertisement
7isenko

G

Mar 27th, 2021 (edited)
679
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4. #include <vector>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. bool cmp(pair<char, int> &a,
  10.          pair<char, int> &b) {
  11.     return a.second > b.second;
  12. }
  13.  
  14.  
  15. int main() {
  16.     string givenWord;
  17.     vector<pair<char, int>> weights;
  18.     map<char, int> entriesCount;
  19.  
  20.     cin >> givenWord;
  21.  
  22.     for (char &c : givenWord) {
  23.         entriesCount[c]++;
  24.     }
  25.  
  26.     givenWord.clear();
  27.  
  28.     char symbol = 'a';
  29.     while (symbol <= 'z') {
  30.         int weight = 0;
  31.         cin >> weight;
  32.         weights.emplace_back(pair<char, int>(symbol, weight));
  33.         symbol++;
  34.     }
  35.  
  36.     sort(weights.begin(), weights.end(), cmp);
  37.  
  38.     string start;
  39.     start.reserve(100000);
  40.     string result;
  41.     result.reserve(100000);
  42.  
  43.     // create result string
  44.     for (auto it = entriesCount.begin(); it != entriesCount.cend();) {
  45.         int amount = it->second;
  46.         if (amount == 1) {
  47.             result.push_back(it->first);
  48.             entriesCount.erase(it++);
  49.             continue;
  50.         }
  51.         if (amount > 2) {
  52.             while (it->second > 2) {
  53.                 result.push_back(it->first);
  54.                 it->second--;
  55.             }
  56.         }
  57.         ++it;
  58.     }
  59.  
  60.     // create start string
  61.     for (auto &i : weights) {
  62.         if (entriesCount[i.first] != 0) {
  63.             start.push_back(i.first);
  64.         }
  65.     }
  66.  
  67.     entriesCount.clear();
  68.     weights.clear();
  69.     result.insert(0, start);
  70.     reverse(start.begin(), start.end());
  71.     result.append(start);
  72.  
  73.     cout << result;
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement