# 有重复元素的排列问题

``````4
aacc``````

``````aacc
acac
acca
caac
caca
ccaa
6``````

``````#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"

int num = 0;
int contains(char list[], int k, int i) {

for (int j = k; j < i; j++) {
if (list[i] == list[j])
return 1;
}
return 0;
}

void Swap(char *a, char *b)
{
char temp = *a;
*a = *b;
*b = temp;
}

void Perm(char list[], int k, int m)
{

if (k == m)
{

for (int i = 0; i <= m; i++)
printf("%c",list[i]);
num++;
printf("\n");
}
else
{

for (int i = k; i <= m; i++)
{
if (!contains(list, k, i))
{
Swap(&list[k], &list[i]);
Perm(list, k+1, m);
Swap(&list[k], &list[i]);
}
}

}
}

int main()
{

int n;
char *list;
scanf("%d",&n);
list = (char *)malloc(sizeof(char)*n);
scanf("%s", list);
Perm(list, 0, n-1);
printf("%d\n",num);
return 0;
}

``````

``````#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"

int num = 0;
int contains(char list[], int k, int i) {

for (int j = k; j < i; j++) {
if (list[i] == list[j])
return 1;
}
return 0;
}

void Swap(char *a, char *b)
{
char temp = *a;
*a = *b;
*b = temp;
}

void Perm(char list[], int k, int m)
{

if (k == m)
{

for (int i = 0; i <= m; i++)
printf("%c",list[i]);
num++;
printf("\n");
}
else
{

for (int i = k; i <= m; i++)
{
if (!contains(list, k, i))
{
Swap(&list[k], &list[i]);
Perm(list, k+1, m);
Swap(&list[k], &list[i]);
}
}

}
}

int main()
{

int n;
char *list;
scanf("%d",&n);
list = (char *)malloc(sizeof(char)*n);
scanf("%s", list);
Perm(list, 0, n-1);
printf("%d\n",num);
return 0;
}

``````