Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- int cnt;
- void swap(char *x, char *y)
- {
- char temp;
- temp = *x;
- *x = *y;
- *y = temp;
- }
- void permute(char *a, int l, int r, int cn)
- {
- int i;
- if (l == r){
- cnt++;
- if(cnt==cn+1)
- printf("%s\n", a);
- }
- else{
- for (i = l; i <= r; i++){
- swap((a+l), (a+i));
- permute(a, l+1, r, cn);
- swap((a+l), (a+i));
- }
- }
- }
- int main()
- {
- int T,i,cn;
- scanf("%d",&T);
- for(i=1;i<=T;i++){
- cnt=0;
- char str[30];
- scanf("%s",str);
- scanf("%d",&cn);
- int n = strlen(str);
- permute(str, 0, n-1,cn);
- printf("\ncount: %d\n",cnt);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement