We are considering a word C containing only lowercase.
An anagram of the word C is a word formed by the letters of C in another order eventually.
E.g.
'armata' is an anagram of the word 'tamara'
but 'maree' is not an anagram of the word 'amare'
A word is an angram of itself.
Determine the number of different anagrams that a word has. Because this number may be very high, there will be posted its decomposition into prime numbers
The input file anagrame.in contains the word C.
The output file anagrame.out will contain the decomposition into prime numbers of the number of anagrams of the word C. On each line there will be a prime factor followed by one space and then its multiplicity(power). The factors will be written in ascending order.
#include <iostream>
#include <algorithm>
#include <set>
#include <string>
std::set<std::string> numAnagrams (std::string& word) {
std::set<std::string> anagrams;
std::sort(word.begin(), word.end());
do anagrams.emplace(word);
while (std::next_permutation(word.begin(), word.end()));
return anagrams;
}
int main() {
std::string word = "hello", originalWord = word;
std::set<std::string> anagrams = numAnagrams(word);
// for (const std::string& x : anagrams) std::cout << x << std::endl; // In case you wanted to see them all.
std::cout << "\nNumber of anagrams of '" << originalWord << "' is " << anagrams.size() << ".\n";
}
Output:
Number of anagrams of 'hello' is 60.
The prime factorization and fstream handling is up to you now.
If you are not allowed to use the algorithm library, then keep swapping the letters of the word, placing each new arrangement into a set (so that duplicates are ignored), until you've returned to the original word, in which case stop.
@prestokeys, this code is especially amusing after warning in OP post about large numbers and efficiency questions.
set for all permutations of informatician would be about 4GB in size.
@OP:
1) find factorisation of numerator of multiset permutation formula and store results in a vector. Sort it . Example:
His problem is about efficient implementation of calculation of number of different anagrams which is reduced to efficient implementation of abstract math alghorithm.