QBHV - Hoán vị chữ cái
Tác giả: skyvn97
Ngôn ngữ: C++
#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();
}