Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- #include <string>
- #include <vector>
- #include <algorithm>
- using namespace std;
- bool cmp(pair<char, int> &a,
- pair<char, int> &b) {
- return a.second > b.second;
- }
- int main() {
- string givenWord;
- vector<pair<char, int>> weights;
- map<char, int> entriesCount;
- cin >> givenWord;
- for (char &c : givenWord) {
- entriesCount[c]++;
- }
- givenWord.clear();
- char symbol = 'a';
- while (symbol <= 'z') {
- int weight = 0;
- cin >> weight;
- weights.emplace_back(pair<char, int>(symbol, weight));
- symbol++;
- }
- sort(weights.begin(), weights.end(), cmp);
- string start;
- start.reserve(100000);
- string result;
- result.reserve(100000);
- // create result string
- for (auto it = entriesCount.begin(); it != entriesCount.cend();) {
- int amount = it->second;
- if (amount == 1) {
- result.push_back(it->first);
- entriesCount.erase(it++);
- continue;
- }
- if (amount > 2) {
- while (it->second > 2) {
- result.push_back(it->first);
- it->second--;
- }
- }
- ++it;
- }
- // create start string
- for (auto &i : weights) {
- if (entriesCount[i.first] != 0) {
- start.push_back(i.first);
- }
- }
- entriesCount.clear();
- weights.clear();
- result.insert(0, start);
- reverse(start.begin(), start.end());
- result.append(start);
- cout << result;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement