C语言中的字符置换算法,将输出存储在数组中
我需要在C中存储四个字母的排列 我试图使用这个算法,但不知道如何将输出存储在某个数组中 如果有人能帮我纠正这一点,或者给出另一个算法,我将不胜感激C语言中的字符置换算法,将输出存储在数组中,c,algorithm,combinations,permutation,C,Algorithm,Combinations,Permutation,我需要在C中存储四个字母的排列 我试图使用这个算法,但不知道如何将输出存储在某个数组中 如果有人能帮我纠正这一点,或者给出另一个算法,我将不胜感激 #include <stdio.h> #include <string.h> void swap(char* x, char* y) { char temp; temp = *x; *x = *y; *y = temp; } void permute(char* a,
#include <stdio.h>
#include <string.h>
void swap(char* x, char* y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
void permute(char* a, int l, int r)
{
int i;
if (l == r)
printf("%s\n", a);
else {
for (i = l; i <= r; i++) {
swap((a + l), (a + i));
permute(a, l + 1, r);
swap((a + l), (a + i)); // backtrack
}
}
}
int main()
{
char str[] = "AGTC";
int n = strlen(str);
permute(str, 0, n - 1);
return 0;
}
您应该注意,您将需要一个相当大的数组来存储所有排列。如果您有一个4字节的字符串,这将是一个24*5的2D数组。因此,只有在您提前知道要支持的字符串的最大大小时,这才是实用的 下面的代码适用于最多4字节的字符串。对于更大的大小,您需要增加2D阵列存储的两个维度。e、 g.对于5字节,它将是120*6
附加说明-上述算法不打印不同的排列。如果输入字符串有重复项,此算法将打印带有重复项的排列。e、 g.如果输入是AAAA,输出是24行AAAA,您可以使用malloc来完成。为此,您需要知道组合的数量。 组合将是给定字符串大小的阶乘
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void swap(char* x, char* y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
void permute(char* a, int l, int r, char arr[], int n)
{
int i;
static long count = 0;
if (l == r)
{
//printf("%s\n", a);
memcpy(arr+count*n, a, n);
count++;
}
else {
for (i = l; i <= r; i++) {
swap((a + l), (a + i));
permute(a, l + 1, r, arr, n);
swap((a + l), (a + i)); // backtrack
}
}
}
long factorial(int n)
{
int c = 0;
long fact = 1;
for (c = 1; c <= n; c++)
fact = fact * c;
return fact;
}
int main()
{
char str[] = "AGTC";
int n = strlen(str);
long t_comb = factorial(n);
char *arr = NULL;
char *print = NULL;
arr = (char *)malloc(t_comb * n);
if(arr == NULL)
{
printf("error\n");
}
print = (char *)malloc(n+1);
memset(print, '\0', n+1);
permute(str, 0, n - 1, arr, n);
long itr = 0;
for(itr = 0 ; itr < t_comb ; itr++)
{
memcpy(print, arr+itr*n, n);
printf("%s\n", print);
}
/* After using */
free(print);
free(arr);
return 0;
}
首先,不能将所有排列存储在一个输出数组中,每个排列需要一个数组。第二,如果您提出一个迭代算法而不是递归算法,可能会更容易。第三,现在交换原始数组中的字符,而不是将字符写入输出数组中的相应位置。最后,不要忘记C中的字符串实际上被称为以null结尾的字节字符串,一个四个字符的字符串需要五个字符的空间来包含终止符。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void swap(char* x, char* y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
void permute(char* a, int l, int r, char arr[], int n)
{
int i;
static long count = 0;
if (l == r)
{
//printf("%s\n", a);
memcpy(arr+count*n, a, n);
count++;
}
else {
for (i = l; i <= r; i++) {
swap((a + l), (a + i));
permute(a, l + 1, r, arr, n);
swap((a + l), (a + i)); // backtrack
}
}
}
long factorial(int n)
{
int c = 0;
long fact = 1;
for (c = 1; c <= n; c++)
fact = fact * c;
return fact;
}
int main()
{
char str[] = "AGTC";
int n = strlen(str);
long t_comb = factorial(n);
char *arr = NULL;
char *print = NULL;
arr = (char *)malloc(t_comb * n);
if(arr == NULL)
{
printf("error\n");
}
print = (char *)malloc(n+1);
memset(print, '\0', n+1);
permute(str, 0, n - 1, arr, n);
long itr = 0;
for(itr = 0 ; itr < t_comb ; itr++)
{
memcpy(print, arr+itr*n, n);
printf("%s\n", print);
}
/* After using */
free(print);
free(arr);
return 0;
}