Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- #include <vector>
- #include <algorithm>
- class Mix
- {
- public:
- struct all_in_one
- {
- int amount = 0;
- char letter;
- int number_of_string = 0;
- };
- static std::string mix(const std::string &s1, const std::string &s2)
- {
- //strings parsing
- std::map<char, int> letters1, letters2;
- for (int i = 0; i < 26; ++i)
- {
- letters1.emplace('a' + i, 0);
- letters2.emplace('a' + i, 0);
- }
- for (const auto item : s1)
- if (std::islower(item))
- letters1[item]++;
- for (const auto item : s2)
- if (std::islower(item))
- letters2[item]++;
- //split all data in to one vector
- std::vector<all_in_one> one_string;
- for ( char c = 'a'; c <= 'z'; ++c)
- {
- if (letters1[c] > 1 && letters1[c] > letters2[c])
- one_string.push_back({letters1[c], c , 1});
- if (letters2[c] > 1 && letters1[c] < letters2[c])
- one_string.push_back({letters2[c], c , 2});
- if (letters2[c] > 1 && letters1[c] == letters2[c])
- one_string.push_back({letters2[c], c , 3});
- }
- //sorting by amount (value) and by char together!!!
- std::sort(one_string.begin(), one_string.end(),
- [](const all_in_one& first, const all_in_one& second) -> bool
- {
- if (first.amount > second.amount)
- return true;
- if (first.amount < second.amount)
- return false;
- if (first.letter == second.letter)
- return false;
- if (first.number_of_string != second.number_of_string)
- return first.number_of_string < second.number_of_string;
- return first.letter < second.letter;
- });
- //just print small letters from s1 and s2 strings in one string
- std::string result = "";
- for (const auto& item : one_string)
- {
- std::string str(item.amount, item.letter);
- if (item.number_of_string == 1 or item.number_of_string == 2)
- result += std::to_string(item.number_of_string) + ":" + str + "/";
- else
- {
- result += "=:" + str + "/";
- }
- }
- if (result.size() > 0)
- result.pop_back();
- return result;
- };
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement