Code for USACO practice problems
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
855 B

#include <algorithm>
#include <iostream>
using namespace std;
long find_fact(int num) {
long num_perms = 1;
for (int i = 0; i < num; i++) {
num_perms *= num - i;
}
return num_perms;
}
int main() {
string m;
cin >> m;
string n = m;
sort(n.begin(), n.end());
long num_perms = 1;
int current_repetition = 1;
for (int i = 0; i < n.length(); i++) {
num_perms *= n.length() - i;
if (i > 0) {
if (n.at(i) == n.at(i - 1)) {
current_repetition += 1;
} else {
if (current_repetition > 1) {
num_perms /= find_fact(current_repetition);
}
current_repetition = 1;
}
}
}
if (current_repetition > 1) {
num_perms /= find_fact(current_repetition);
}
cout << num_perms << endl;
do {
cout << n << endl;
} while (next_permutation(n.begin(), n.end()));
}