Advertisement
erfanul007

bactracking permutation

Dec 4th, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. int cnt;
  4. void swap(char *x, char *y)
  5. {
  6. char temp;
  7. temp = *x;
  8. *x = *y;
  9. *y = temp;
  10. }
  11.  
  12. void permute(char *a, int l, int r, int cn)
  13. {
  14. int i;
  15. if (l == r){
  16. cnt++;
  17. if(cnt==cn+1)
  18. printf("%s\n", a);
  19. }
  20. else{
  21. for (i = l; i <= r; i++){
  22. swap((a+l), (a+i));
  23. permute(a, l+1, r, cn);
  24. swap((a+l), (a+i));
  25. }
  26. }
  27. }
  28.  
  29. int main()
  30. {
  31. int T,i,cn;
  32. scanf("%d",&T);
  33. for(i=1;i<=T;i++){
  34. cnt=0;
  35. char str[30];
  36. scanf("%s",str);
  37. scanf("%d",&cn);
  38. int n = strlen(str);
  39. permute(str, 0, n-1,cn);
  40. printf("\ncount: %d\n",cnt);
  41. }
  42. return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement