Given a string of letters (A-Z), your task is to arrange them in alphabetic order.
Following is an example:
A string "BAC" contains 3 letters B, A and C, you shoshould output
ABC
ACB
BAC
BCA
Cab
CBA
In the output file.
A string may contain several letters same, for example "BBC" You shoshould output like this:
BBC
BCB
CBB
Input
The first line of input contains a single integer t, the number of test cases, followed by the input data for each test data. each test case is a string of N (1 <= n <= 26) letters.
Output
You shoshould output case K: in the first line and the sequences of arrangement in the following lines of each case.
Sample Input
2
BAC
BBC
Sample output
Case 1:
ABC
ACB
BAC
BCA
Cab
CBA
Case 2:
BBC
BCB
CBB
This question is implemented by recursive algorithms. Chain string & nbsp; structure;
The algorithm is described as follows: For a chain string, there is a pointer marked to start replacement, and then moves backward until the end, and then the replaced pointer moves forward to repeat the above process. If the content of the marked pointer is different from that of the mobile pointer, it is printed;
The source code is as follows:
# Include <stdio. h>
# Include <iostream>
Using namespace STD;
Typedef struct Str
{
Char ch;
Struct Str * next;
} STR;
Void print (Str * s); // print
Void create (Str * & S); // create
Void travel (Str * & S, Str * r); // traversal. recursive algorithms are used to solve the problem of arrangement.
Void swap (char & A, char & B); // exchange
Int main (INT argc, char * argv [])
{
Str * s;
Create (s );
Print (s );
Travel (S, S-> next );
System ("pause ");
Return 0;
}
Void create (Str * & S) // create a chain string
{
S = new STR;
Str * P;
Str * q = s;
Char ch;
While (CH = getchar ())! = '/N ')
{
P = new STR;
P-> CH = CH;
P-> next = NULL;
Q-> next = P;
Q = P;
}
Q-> next = NULL;
}
Void print (Str * s)
{
Str * P;
P = s-> next;
While (P! = NULL)
{
Cout <p-> CH;
P = p-> next;
}
Putchar ('/N ');
}
Void swap (char & A, char & B)
{
Char temp;
Temp =;
A = B;
B = temp;
}
Void travel (Str * & S, Str * r) // traverse
{
Str * P = R;
Str * q = p-> next;
If (R-> next = NULL)
{
Return;
}
Else
{
While (P! = NULL)
{
Q = p-> next;
While (Q! = NULL)
{
Swap (p-> CH, Q-> CH );
If (p-> CH! = Q-> CH)
{
Print (s );
Travel (S, P-> next );
}
Swap (p-> CH, Q-> CH );
Q = Q-> next;
}
P = p-> next;
}
}
}