An ACM question about string arrangement --- It seems to be an interview question of thunder

Source: Internet
Author: User

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;

}
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.