• Source
    1. // Author: Ashis Kumar Chanda
    2. // CSE, DU
    3.  
    4. //An upper case letter goes before the corresponding lower case letter.
    5. // special case: input wArZB
    6. // output ABZrw is wrong. it will ABrwZ
    7.  
    8. // we need to sort: guess char as in that order
    9. // A B C a b c
    10. // 0 2 4 1 3 5
    11.  
    12.  
    13. #include<iostream>
    14. #include<algorithm>
    15. #include<stdio.h>
    16. #include<string.h>
    17. using namespace std;
    18.  
    19. int value[127];
    20. struct cmp {
    21. bool operator()(int x, int y) {
    22. return value[x] < value[y];
    23. }
    24. };
    25.  
    26. int main()
    27. {
    28. int i,k, t;
    29. char str[1009];
    30.  
    31. for(k = 'A', i=0; k <= 'Z' ; ++k,++i){
    32. value[k] = i;
    33. value[k+32] = ++i;
    34. }
    35.  
    36. scanf("%d", &t);
    37. while(t--){
    38. scanf("%s",str);
    39. k= strlen( str);
    40. sort( str, str+k, cmp() );
    41.  
    42. do{
    43. printf("%s\n", str);
    44. }while( next_permutation( str, str+ k, cmp() ) );
    45. }// next permutation also work at ascending order, so we add cmp where also
    46. return 0;
    47. }