added better permutations
This commit is contained in:
parent
2b362d0916
commit
96b4d2e9eb
@ -1,94 +1,59 @@
|
|||||||
#include <unistd.h> // write
|
#include <stdlib.h>
|
||||||
#include <stdlib.h> // malloc, free
|
#include <stdio.h>
|
||||||
|
|
||||||
int ft_strlen(const char *s)
|
int length;
|
||||||
|
char *set;
|
||||||
|
char *permutation;
|
||||||
|
|
||||||
|
int ft_strlen(char *s)
|
||||||
{
|
{
|
||||||
int len = 0;
|
int i = 0;
|
||||||
while (s[len])
|
while(s[i])
|
||||||
len++;
|
i++;
|
||||||
return (len);
|
return (i);
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_solution(const char *s)
|
void permutations(int index, char *used)
|
||||||
{
|
{
|
||||||
write(1, s, ft_strlen(s));
|
if (index == length)
|
||||||
write(1, "\n", 1);
|
{
|
||||||
|
printf("%s\n", permutation);
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
while (i < length)
|
||||||
|
{
|
||||||
|
if (used[i] == '0')
|
||||||
|
{
|
||||||
|
permutation[index] = set[i];
|
||||||
|
used[i] = '1';
|
||||||
|
permutations(index + 1, used);
|
||||||
|
used[i] = '0';
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ft_swap(char *a, char *b)
|
char *populate_used()
|
||||||
{
|
{
|
||||||
char temp = *a;
|
char *ret = (char *) calloc (length + 1, sizeof(char));
|
||||||
*a = *b;
|
int i = 0;
|
||||||
*b = temp;
|
while (i < length)
|
||||||
|
ret[i++] = '0';
|
||||||
|
return (ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
void sort_string(char *str, int n)
|
int main(int ac, char **av)
|
||||||
{
|
{
|
||||||
int i;
|
if (ac != 2)
|
||||||
int j;
|
return (1);
|
||||||
|
set = av[1];
|
||||||
i = 0;
|
length = ft_strlen(av[1]);
|
||||||
while (i < n - 1)
|
char *used = populate_used();
|
||||||
{
|
permutation = (char *) malloc(sizeof(char) * (length + 1));
|
||||||
j = 0;
|
permutations(0, used);
|
||||||
while (j < n - i - 1)
|
free(used);
|
||||||
{
|
free(permutation);
|
||||||
if (str[j] > str[j + 1])
|
return (0);
|
||||||
ft_swap(&str[j], &str[j + 1]);
|
|
||||||
j++;
|
|
||||||
}
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void generate_permutations(char *original_str, char *current_perm, int *used, int n, int k)
|
|
||||||
{
|
|
||||||
if (k == n)
|
|
||||||
{
|
|
||||||
print_solution(current_perm);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int i = 0;
|
|
||||||
while (i < n)
|
|
||||||
{
|
|
||||||
if (used[i] == 0)
|
|
||||||
{
|
|
||||||
current_perm[k] = original_str[i];
|
|
||||||
used[i] = 1;
|
|
||||||
generate_permutations(original_str, current_perm, used, n, k + 1);
|
|
||||||
used[i] = 0;
|
|
||||||
}
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
|
||||||
{
|
|
||||||
if (argc != 2 || !argv[1][0])
|
|
||||||
return (0);
|
|
||||||
|
|
||||||
int n = ft_strlen(argv[1]);
|
|
||||||
char *original_str = argv[1];
|
|
||||||
|
|
||||||
sort_string(original_str, n);
|
|
||||||
|
|
||||||
char *current_perm = (char *)malloc(sizeof(char) * (n + 1));
|
|
||||||
if (!current_perm)
|
|
||||||
return (1);
|
|
||||||
current_perm[n] = '\0';
|
|
||||||
|
|
||||||
int *used = (int *)calloc(sizeof(int) * n);
|
|
||||||
if (!used)
|
|
||||||
{
|
|
||||||
free(current_perm);
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
|
|
||||||
generate_permutations(original_str, current_perm, used, n, 0);
|
|
||||||
|
|
||||||
free(current_perm);
|
|
||||||
free(used);
|
|
||||||
|
|
||||||
return (0);
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user