## Editorial for Hoán vị chữ cái

Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này

#### Code mẫu của flashmt

var d:array['A'..'Z'] of byte;
com:array[1..9,0..9] of longint;
s:string;
n:byte;
dem:longint;

procedure init;
var i,j:byte; c:char;
begin
fillchar(com,sizeof(com),0);
for i:=1 to n do
begin
com[i,0]:=1; com[i,i]:=1;
end;
for i:=2 to n do
for j:=1 to i-1 do
com[i,j]:=com[i-1,j]+com[i-1,j-1];
dem:=1;
i:=n;
for c:='A' to 'Z' do
if d[c]>0 then
begin
dem:=dem*com[i,d[c]];
i:=i-d[c];
end;
end;

procedure rf;
var c:char;
begin
n:=0;
fillchar(d,sizeof(d),0);
while not eof do
begin
inc(n);
inc(d[c]);
end;
end;

procedure att(i:byte);
var c:char; j:byte;
begin
for c:='A' to 'Z' do
begin
if d[c]>0 then
begin
dec(d[c]);
s:=s+c;
if i<n then att(i+1)
else writeln(s);
inc(d[c]);
delete(s,i,1);
end;
end;
end;

procedure pr;
var i:byte;
begin
init;
s:='';
writeln(dem);
att(1);
end;

begin
rf;
pr;
end.

#### Code mẫu của happyboy99x

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<int> vi;
typedef vector<vi> vvi;

#define sz(a) int((a).size())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define tr(c,i) for(typeof((c).begin()) i = (c).begin(), _e = (c).end(); i != _e; ++i)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define rep(i,n) for(int i = 0, _n = (n); i < _n; ++i)
#define repd(i,n) for(int i = (n)-1; i >= 0; --i )
#define fo(i,a,b) for(int i = (a), _b = (b); i <= _b; ++i)
#define fod(i,a,b) for(int i = (a), _b = (b); i >= _b; --i)

#define INF 1000000000
#define N

int main() {
string s; cin >> s;
vector<string> v; sort(all(s)); v.pb(s);
while(next_permutation(all(s))) v.pb(s);
printf("%d\n",v.size());
tr(v,it) puts(it->c_str());
return 0;
}

program qbhv;
uses    math;
const   fi='';
var     ch:array['A'..'Z'] of longint;
gt:array[1..10] of longint;
res:array[1..10] of char;
s:string[11];
inp:text;
len,counter:longint;

procedure input;
begin
assign(inp,fi);
reset(inp);
len:=length(s);
close(inp);
end;

procedure init;
var     i:longint;
begin
for i:=1 to len do
begin
inc(ch[s[i]]);
end;
gt[1]:=1;
for i:=2 to 9 do
gt[i]:=gt[i-1]*i;
end;

procedure calc;
var     c:char;
begin
counter:=gt[len];
for c:='A' to 'Z' do
if ch[c]>0 then
counter:=counter div gt[ch[c]];
end;

procedure print;
var     i:longint;
begin
for i:=1 to len do
write(res[i]);
writeln;
end;

procedure duyet(i:longint);
var     c:char;
begin
if i>len then
begin
print;
exit;
end;
for c:='A' to 'Z' do
if ch[c]>0 then
begin
dec(ch[c]);
res[i]:=c;
duyet(i+1);
inc(ch[c]);
end;
end;

begin
input;
init;
calc;
writeln(counter);
duyet(1);
end.

#### Code mẫu của RR

var
cnt:longint;
a:string;

function find:longint;
var
i:longint;
begin
for i:=length(a)-1 downto 1 do
if a[i]<a[i+1] then exit(i);
exit(0);
end;

procedure sort(start,last:longint);
var
i,j:longint;
tmp:char;
begin
for i:=start to last-1 do
for j:=i+1 to last do
if a[j]<a[i] then
begin
tmp:=a[i];
a[i]:=a[j];
a[j]:=tmp;
end;
end;

procedure gen(print:boolean);
var
i,j,ln:longint;
tmp,c:char;
begin
i:=find;
cnt:=1; if print then writeln(a);
while i<>0 do
begin
ln:=i;
for j:=i to length(a) do
if (a[j]>a[i]) and ((a[j]<a[ln]) or (ln=i)) then ln:=j;

tmp:=a[ln]; a[ln]:=a[i]; a[i]:=tmp;
sort(i+1,length(a));
i:=find;
inc(cnt); if print then writeln(a);
end;
end;

begin
sort(1,length(a));

gen(false);
writeln(cnt);
sort(1,length(a));
gen(true);
end.

#### Code mẫu của ll931110

Program QBHV;
Const
input  = '';
output = '';
Var
s: string;
n: integer;
x: array[1..9] of char;
d: array['A'..'Z'] of integer;
fac: array[0..9] of longint;
fi,fo: text;

Procedure openfile;
Begin
Assign(fi, input);
Reset(fi);

Assign(fo, output);
Rewrite(fo);
End;

Procedure init;
Var
i: integer;
Begin
n:= length(s);

Fillchar(d, sizeof(d), 0);
For i:= 1 to n do inc(d[s[i]]);
End;

Procedure factorial;
Var
i: integer;
Begin
fac[0]:= 1;
For i:= 1 to 9 do fac[i]:= fac[i - 1] * i;
End;

Procedure number;
Var
res: longint;
ch: char;
Begin
res:= fac[n];
For ch:= 'A' to 'Z' do res:= res div fac[d[ch]];
Writeln(fo, res);
End;

Procedure printresult;
Var
i: integer;
Begin
For i:= 1 to n do write(fo, x[i]);
Writeln(fo);
End;

Procedure attempt(i: integer);
Var
ch: char;
Begin
For ch:= 'A' to 'Z' do
if d[ch] > 0 then
Begin
x[i]:= ch;

If i = n then printresult else
Begin
dec(d[ch]);
Attempt(i + 1);
inc(d[ch]);
End;
End;
End;

Procedure closefile;
Begin
Close(fi);
Close(fo);
End;

Begin
openfile;
init;
factorial;
number;
attempt(1);
closefile;
End.

#### Code mẫu của skyvn97

#include<stdio.h>
#include<string.h>
#define MAXL   20
#define MAXP   362890
long cntp;
int n;
char s[MAXL];
int perm[MAXP][MAXL];
int cnt[MAXL+20];
int cx[MAXL+20];
int x[MAXL];
void init(void)
{
gets(s);
int i;
n=strlen(s);
for (i=0;i<n;i=i+1)
cnt[s[i]-64]++;
cntp=0;
}
void save(void)
{
cntp++;
int i;
for (i=1;i<=n;i=i+1) perm[cntp][i]=x[i];
}
void btrk(int k)
{
int i;
for (i=1;i<=30;i=i+1)
if (cx[i]+1<=cnt[i])
{
x[k]=i;
cx[i]++;
if (k==n) save();
else btrk(k+1);
cx[i]--;
}
}
void print(void)
{
printf("%ld\n",cntp);
long i,j;
for (i=1;i<=cntp;i=i+1)
{
for (j=1;j<=n;j=j+1) printf("%c",perm[i][j]+64);
printf("\n");
}
}
int main(void)
{
init();
btrk(1);
print();
}

#### Code mẫu của khuc_tuan

#include <stdio.h>
#include <iostream>
using namespace std;

int main() {
char a[100];
gets(a);
int na = strlen(a);
sort( a, a+na);
int dem = 0;
do { ++dem; } while(next_permutation(a,a+na));
sort( a, a+na);
printf("%d\n", dem);
do { puts(a); } while(next_permutation(a,a+na));
return 0;
}