#include <iostream> #include <vector> #include <string> #include <map> using namespace…
- User
#include #include #include #include
using namespace std;
map<char, int> countCharacters(const string& str) { map<char, int> charCount; for (char c : str) { charCount[c]++; } return charCount; }
bool matches(const string& mixedString, const vector& selectedWords) { string combined; for (const string& word : selectedWords) { combined += word; } return countCharacters(combined) == countCharacters(mixedString); }
// Рекурсивная функция для поиска подходящих слов void findWords(const string& mixedString, const vector& words, vector& currentWords, int start) { if (currentWords.size() > 8) return; // Ограничение по количеству слов if (matches(mixedString, currentWords)) { for (const string& word : currentWords) { cout << word << endl; } }
codefor (int i = start; i < words.size(); i++) { currentWords.push_back(words[i]); findWords(mixedString, words, currentWords, i + 1); currentWords.pop_back(); }}
int main() { string mixedString; int n;
code// Считываем входные данные cin >> mixedString; cin >> n; vector<string> words(n); for (int i = 0; i < n; i++) { cin >> words[i]; } vector<string> currentWords; findWords(mixedString, words, currentWords, 0); return 0;}
Ускорь этот код немного, упрости
- Assistant