Sample Input 0
2
ab
cd
Sample Output 0
ab cd
cd ab
Sample Input 1
3
a
bc
bc
Sample Output 1
a bc bc
bc a bc
bc bc a
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int next_permutation(int n, char **s){
// Find non-increasing suffix
int i = n-1;
while(i>0 && strcmp(s[i-1],s[i])>=0)
i--; // find key
if (i<=0) return 0;
// Swap key with its successor in suffix
int j = n-1;
while(strcmp(s[i-1],s[j])>=0)
j--; // find rightmost successor to key
char *tmp = s[i-1];
s[i-1] = s[j];
s[j] = tmp;
// Reverse the suffix
j = n-1;
while(i<j) {
tmp = s[i];
s[i] = s[j];
s[j] = tmp;
i++;
j--;
}
return 1;
}
int main()
{
char **s;
int n;
scanf("%d", &n);
s = calloc(n, sizeof(char*));
for (int i = 0; i < n; i++)
{
s[i] = calloc(11, sizeof(char));
scanf("%s", s[i]);
}
do
{
for (int i = 0; i < n; i++)
printf("%s%c", s[i], i == n - 1 ? '\n' : ' ');
}
while (next_permutation(n, s));
for (int i = 0; i < n; i++)
free(s[i]);
free(s);
return 0;
}
2
ab
cd
Sample Output 0
ab cd
cd ab
Sample Input 1
3
a
bc
bc
Sample Output 1
a bc bc
bc a bc
bc bc a
Solution in C:-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int next_permutation(int n, char **s){
// Find non-increasing suffix
int i = n-1;
while(i>0 && strcmp(s[i-1],s[i])>=0)
i--; // find key
if (i<=0) return 0;
// Swap key with its successor in suffix
int j = n-1;
while(strcmp(s[i-1],s[j])>=0)
j--; // find rightmost successor to key
char *tmp = s[i-1];
s[i-1] = s[j];
s[j] = tmp;
// Reverse the suffix
j = n-1;
while(i<j) {
tmp = s[i];
s[i] = s[j];
s[j] = tmp;
i++;
j--;
}
return 1;
}
int main()
{
char **s;
int n;
scanf("%d", &n);
s = calloc(n, sizeof(char*));
for (int i = 0; i < n; i++)
{
s[i] = calloc(11, sizeof(char));
scanf("%s", s[i]);
}
do
{
for (int i = 0; i < n; i++)
printf("%s%c", s[i], i == n - 1 ? '\n' : ' ');
}
while (next_permutation(n, s));
for (int i = 0; i < n; i++)
free(s[i]);
free(s);
return 0;
}
No comments:
Post a Comment
Thanks..